MUTEX in MQL5. Is it possible?

 

Hi guys,

 I need to use a semaphore mutex in my MQL5 code.

Do the language provide us this possibilite without to use a DLL? 

Regards 

 

I'm sorry guys. I was thinking that each EA run in the same program, but in different Threads. But I realize that each one is a program.

Well, If I am right, I won't need MUTEX to control critical regions.

Thanks 

 
humbertobrandao:

I'm sorry guys. I was thinking that each EA run in the same program, but in different Threads. But I realize that each one is a program.

Well, If I am right, I won't need MUTEX to control critical regions.

Thanks 

Each EA runs in his own thread.

If you want a mutex, you have to code it, what's the problem ?

 

Because to implement a mutex, I need to call a special function that runs a System Call on OS. But I don't know if this function exists in MQL5.

After I sent my question here, I realized that each EA is running in different Threads. So, I think I don't need to control critical regions, because they are separated for each EA. Am I right?

 
humbertobrandao:

Because to implement a mutex, I need to call a special function that runs a System Call on OS. But I don't know if this function exists in MQL5.

We don't have special function for mutex in mql5. But as I said, you can code it yourself, no need for a special function or a dll.

After I sent my question here, I realized that each EA is running in different Threads. So, I think I don't need to control critical regions, because they are separated for each EA. Am I right?

I already confirm that each runs in his own thread.

What is "critical regions" ?

 

What is "critical regions" ?

http://en.wikipedia.org/wiki/Critical_section
Critical section - Wikipedia, the free encyclopedia
Critical section - Wikipedia, the free encyclopedia
  • en.wikipedia.org
In concurrent programming, a critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution.1 A critical section will usually terminate in fixed time, and a thread, task, or process will have to wait for a fixed time to enter it (aka...
 
humbertobrandao:
http://en.wikipedia.org/wiki/Critical_section

lol.

This was not my question, what is your critical section, concretely.

 
angevoyageur:

lol.

This was not my question, what is your critical section, concretely.

Let's imagine that I have a counter of unique identificators for different market makers that I trigger, in the same EA.

I can trigger a new market maker in two different functions:

Both are called by MT5, when different events come.

In my case, both need to get the next unique ID for execute a market maker.

In this context, my critical secction is something like this:

int nextMarketMakerId(){
   return ++_nextMMId;
}

 In java, for example, I could emulate a mutex using the reserved word "synchronized" like

public synchronized int nextMarketMakerId(){
   return ++_nextMMId;
}

 In C, I could call the function pthread_mutex_lock of pthread.h library like

#include <threads.h>
 
static mtx_t cs_mutex;
 
void nextMMId( void ){
    int result = -1;
    mtx_lock( &cs_mutex );
    result = ++_nextMMId;
    mtx_unlock( &cs_mutex );
    return result;
}

And my question is about this. Maybe it can be divide in two questions:

  • In MQL5, Can the MT5 call the two functions (OnTradeTransaction and OnTimer) at the same time? Or Will they be called without time intersection?
  • How can I use a mutex structure in MT5.

 Do you get it?

Thanks! 

 
humbertobrandao:

Let's imagine that I have a counter of unique identificators for different market makers that I trigger, in the same EA.

I can trigger a new market maker in two different functions:

  • OnTradeTransaction;
  • OnTimer.

Both are called by MT5, when different events come.

In my case, both need to get the next unique ID for execute a market maker.

In this context, my critical secction is something like this:

 In java, for example, I could emulate a mutex using the reserved word "synchronized" like

 In C, I could call the function pthread_mutex_lock of pthread.h library like

And my question is about this. Maybe it can be divide in two questions:

  • In MQL5, Can the MT5 call the two functions (OnTradeTransaction and OnTimer) at the same time? Or Will they be called without time intersection?

No, they cannot be called at the same time. 1 EA use 1 thread, all the code is executed in 1 thread.

See documentation :

A program receives only events from the chart it runs on. All events are processed one after another in the order they are received. If a queue already has a NewTick event, or this event is currently being processed, then the new NewTick event is not placed in the queue of the MQL5 program. Similarly, if ChartEvent is already enqueued, or this event is being processed, no new event of this kind is enqueued. The timer events are handled the same way – if the Timer event is in the queue or being handled, the new timer event is not enqueued.

  • How can I use a mutex structure in MT5.

You don't need a mutex here. You will eventually need it if your code is accessing something from different EA. For example, global variables of the terminal or a file, etc...

If you really need a mutex, I am sure you will be able to imagine a solution

 

Many thanks angevoyageur.

:) 

 

One question,  If 1 EA runs on 1 thread, then it seems onTimer won't be called while onTick has some blocking function call or sleep more than timer interval??

Why don't MT5 separating timer event thread? because of Mutex implementation?