Mutex - WinAPI - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources
why this is necessary
Global variables, disk or memory mapped files, and important operating procedures are no longer a problem!
You will learn how important it is that the algorithms you execute do not process the same data through different processes at the same time. Because it may cause unexpected effects: one process is writing, another process is reading and making some decisions, and updating this data at the same time.
In this case, if the first process's data writing has not completed, it may cause the second process to work incorrectly, resulting in data corruption.
We use the object MutvarEx in other words, which contains the operating system mutex.
Mutexes are very simple. We use a principle similar to that described in this article : resources are locked when an object is created and released when the object is deleted.
The use of mutex is implemented in two classes.
The thing is, the mutex releases the Mutex resource and deletes it after '}' . In this way the necessary flexibility is achieved
//------------------------------------------------------------------ Class CMutexSync class mutex synchronization { HANDLE64 m_mutex; // Descriptor of the mutex being created people : CMutexSync() { m_mutex= invalid ; } virtual ~CMutexSync() { destroy(); } boolean create(LPCTSTR name) { m_mutex = CreateMutexWX( 0 , false , name); return (m_mutex ! = invalid ); } void destroy() { CloseHandleX(m_mutex); m_mutex = invalid ; } HANDLE64 get() constant { return (m_mutex); } }; //------------------------------------------------------------------ Class CMutexLock class mutex { HANDLE64 m_mutex; // locked mutex descriptor people : CMutexLock(CMutexSync &m, DWORD dwWaitMsec) { m_mutex=m.Get(); constant DWORD res=WaitForSingleObjectX(m_mutex, dwWaitMsec); } // Capture the mutex during object construction ~CMutexLock() { ReleaseMutexX(m_mutex); } // Release the mutex when the object is removed };
Due to the locking principle of the mutex, the code is beautiful and readable. It frees code from WinAPI functions
In this example, we will synchronize the loop operations of two scripts in two charts.
The first script takes too long to perform some operations (2 seconds in the example). The second script performs the operation quickly (0.2 seconds).
Our task is to not allow new iterations of the express script until the first script completes the current iteration .
Input integer sleep = 200 ; //---------------------------------------------------------------------------------- blank start () { sleep ( 5000 ); // Wait for the script to appear on the second chart CMutexSync sync; // sync object if (!sync.Create( "local\test" )) { print ( symbol () + "MutexSync creation error!" ); return ; } print ( symbol () + "MutexSync created successfully! sleep=" , sleep); for ( integer i = 0 ; i < 10 ; i++) { CMutexLock lock(synchronized, (DWORD) INFINITE); // Lock a segment in this bracket of the loop // Here the wait for the mutex occurs and the lock prints ( symbol () + "lock range" ); sleep (sleep); } }
result
Mutex (EURUSD,M1) EURUSD MutexSync created successfully! sleep=2000
Mutex (EURUSD,M1) EURUSD Locking range mutex (EURUSD,M1) EURUSD Locking range mutex (AUDCAD,H1) AUDCAD MutexSync was created successfully! sleep=200
Mutex (AUDCAD,H1) AUDCAD Locking range mutex (EURUSD,M1) EURUSD Locking range mutex (AUDCAD,H1) AUDCAD Locking range mutex (EURUSD,M1) EURUSD Locking range mutex (AUDCAD,H1) AUDCAD Locking range mutex (EURUSD,M1) EURUSD Locking range mutex (AUDCAD,H1) AUDCAD Locking range mutex (EURUSD,M1) EURUSD locking range mutex (AUDCAD,H1) AUDCAD locking range mutex (EURUSD,M1) EURUSD locking range mutex (AUDCAD,H1) AUDCAD locking range mutex (EURUSD,M1) EURUSD locking range mutex (AUDCAD,H1) AUDCAD locking range mutex (EURUSD,M1) EURUSD lock range mutex (AUDCAD,H1) AUDCAD lock range mutex (EURUSD,M1) EURUSD lock range mutex (AUDCAD,H1) AUDCAD lock range
The library works like a memory map with 32/64 bit counting.
Wish you good luck and great profits!
Attachment download
📎 mutex.mqh (3.26 KB)
📎 mutex.mq5 (1.21 KB)
Source: MQL5 #1835
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •