MT4 Thread structure... need to know.

 

From the docs it's evident the order code which relates to the server is on a different thread.

From the docs it's also evident that indicators work on the same GUI thread.


What about different EAs?

Is there a different thread each EA attached to a chart?


My issue is that I need to know if I am going to have troubles when calling external DLL in which I use non-thread-safe stuff.


Thanks.

 
Marzullo:

Is there a different thread each EA attached to a chart?

From what I've seen, each tick within an EA seems to get handled on a new thread. It doesn't look as though MT4 operates on the basis of creating a single, constant thread for each EA. Nor does MT4 seem to have a pool of worker threads (at least, not in this context). It seems as though a new thread is created in order to process each call to start(), and the thread is presumably destroyed when start() ends.


But I may be wrong. If the above were correct, you'd expect it to be because start() is re-entrant. Since start() isn't re-entrant, it's a bid odd that an EA isn't assigned to a single thread throughout its lifetime.

 
Marzullo wrote >>

From the docs it's evident the order code which relates to the server is on a different thread.

From the docs it's also evident that indicators work on the same GUI thread.

What about different EAs?

Is there a different thread each EA attached to a chart?

My issue is that I need to know if I am going to have troubles when calling external DLL in which I use non-thread-safe stuff.

Thanks.

You should implement synchronization code that only allows an exclusive access to your dll. As illustrated in 'Error 146 ("Trade context busy") and How to Deal with It', whenever two experts try simultaneously access trading functions, there is a "trade context busy" error 146. This shows that each EA has its own thread.

 

AM

> As illustrated in 'Error 146 ("Trade context busy") and How to Deal with It', whenever two experts try simultaneously access trading functions, there is a "trade context busy" error 146.

> This shows that each EA has its own thread

Too right - something newbies should be aware of if they are running more than one EA

-BB-

 
abstract_mind:

You should implement synchronization code that only allows an exclusive access to your dll. As illustrated in 'Error 146 ("Trade context busy") and How to Deal with It' [...]

This is an excellent point, and well worth bearing in mind.


However, in relation to Marzullo's specific question, it's not necessarily going to solve potential problems in a DLL caused by an assumption that the DLL will repeatedly be called in the context of the same thread. It depends whether the non-thread-safe stuff is non-thread-safe because of re-entrancy, or because of thread-local storage (or, indeed, something else).

 
abstract_mind:

You should implement synchronization code that only allows an exclusive access to your dll. As illustrated in 'Error 146 ("Trade context busy") and How to Deal with It', whenever two experts try simultaneously access trading functions, there is a "trade context busy" error 146. This shows that each EA has its own thread.

Could also be that I have 1 thread which handles orders and 1 thread which manages all EAs, if I send an order with EA-1, then my thread goes into another EA-2 and while trying to send an order then it gets 146 since the orders thread is still busy with order sent from EA-1 ... That "simultaneously" suggests the "1 thread per EA solution".

Anyway to be 100% I think I should synch every potentially critical stuff in my DLL.



 
jjc:

This is an excellent point, and well worth bearing in mind.


it's not necessarily going to solve potential problems in a DLL caused by an assumption that the DLL will repeatedly be called in the context of the same thread.


How could you be sure about it? I mean the fact that only one thread is executing all my EAs ...


I will implement a DLL and ask for thread ID, then start 50 EAs and look if it's the same or not... that's it.

But even if we're in the same thread, MT4 versions change... so this in future could also be a wrong assumption... potentually dangerous for our automatic trading..., mind if something goes wrong in the DLL, the entire process crashes... and your money could be in danger.

 
Marzullo:

How could you be sure about it? I mean the fact that only one thread is executing all my EAs ...

Use of GetCurrentThreadId() suggests that each call to a DLL comes from a different thread. As I've already said, it looks as though these threads are disposable, with MT4 creating a new thread to handle each call to start().


If your DLL is non-thread-safe in the sense that it doesn't like concurrent calls from different threads, then I'd suggest that you either use the method mentioned above in the article about error 146, or that you wrap the relevant areas of the DLL in something like a critical section. Preferably both, for safety. (I heartily recommend the article about error 146. Even without using DLLs I've had some very odd problems in MT4 which were solved by the recommendations in this article. But this does come at a price: the article is proposing something which is effectively cooperative multi-tasking.)


If your DLL is non-thread-safe in the sense that it expects a caller to use a single, consistent thread - and correspondingly allocates per-client data using something like __declspec(thread) - then you'll need to change the DLL code and remove this assumption.

Reason: