Threads

In a simplified form, a program can be represented as a sequence of statements that a developer has generated for a computer. The main executor of statements in a computer is the central processing unit. Modern computers are usually equipped with processors with multiple cores, which is equivalent to having multiple processors. However, the number of programs a user may want to run in parallel is virtually unlimited. Thus, the number of programs is always many times greater than the available cores/processors. Due to this, each core actually divides its working time between several different programs: it will allocate 1 millisecond for executing the statements of one program, then 1 millisecond for the statements of another, then for thirds, and so on, in a circle. Since the switching occurs very quickly, the user does not notice this, as it seems that all programs are executed in parallel and simultaneously.

For the processor to be able to suspend the execution of the statements of one program and then resume its work from the previous place (after it quietly switched to the statements of other "parallel" programs), it must be able to somehow save and restore the intermediate state of each program: the current statement, variables, possibly open files, network connections, and so on. This entire collection of resources and data that a program needs to run normally, along with its current position in the sequence of statements, is called the program's execution context. The operating system, in fact, is designed to create such contexts for each program at the request of the user (or other programs). Each such active context is called a thread. Many programs require many threads for themselves because their functionality involves maintaining several activities in parallel. MetaTrader 5 also requires multiple threads to load loading quotes for multiple symbols, plot charts, and respond to user actions. Furthermore, separate threads are also allocated to MQL programs.

The MQL program execution environment allocates no more than one thread to each program. Expert Advisors, scripts, and services receive strictly one thread each. As for indicators, one stream is allocated for all indicators working on one financial instrument. Moreover, the same thread is responsible for displaying the charts of the corresponding symbol, so it is not recommended to occupy it with heavy calculations. Otherwise, the user interface will become unresponsive: user actions will be processed with a delay, or the window will even become unresponsive. Threads of all other types of MQL programs are not tied to an interface and, therefore, can load the processor with any complex task.

One of the important properties of a thread follows from its definition and purpose: It only supports sequential execution of specified statements one after another. Only one statement is executed in one thread at a time. If an infinite loop is written in the program, the thread will get stuck on this instruction and never get to the instructions below it. Long calculations can also create the effect of an endless loop: they will load the processor and prevent other actions from being performed, the results of which the user may expect. That is why efficient calculations in indicators are important for the smooth operation of the graphical interface.

However, in other types of MQL programs, attention should be paid to thread arrangement. In the following sections, we will get familiar with the special event handling functions that are the entry points to MQL programs. A single-threaded model means that during the processing of one event, the program is immune to other events that could potentially occur at the same time. Therefore, the terminal organizes an event queue for each program. We will touch on this point in more detail in the next section.

In order to experience the effects of single-threading in practice, we will look at a simple example in the section Limitations and benefits of indicators (IndBarIndex.mq5). We have chosen indicators for this purpose because they not only share one thread for each symbol but also display results directly on the chart, which makes the potential problem the most obvious.