Mutex - Win AP I
This tool for MetaTrader 5 is specifically engineered to streamline your trading operations. This library provides a collection of modular, reusable code. It is utilized by developers to organize common functions, allowing for the integration of complex logic across multiple Expert Advisors, indicators, or scripts without the need for code duplication.
How to Setup and Use Mutex - Win AP I
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
Description & Settings
Why this is necessary
Global variables, disk or memory-mapped files, important operating procedure are not a problem anymore!
If you use:
calls to the same data from
different
experts
work with
other
applications from MQL in general data
your copiers work in various MT4-MT4 / MT4-MT5 / MT5-MT5 terminals (Hello, Urain :)
or you just need to comply with exact operating procedures among different experts of one or
different
terminals,
you shall understand how important is that performed algorithms do not appeal to same data via different processes simultaneously. As it can result in unexpected effects: one process is writing, and the other one is reading and making some decision, and renewing these data at the same time.
In such a case if writing of data by first process hasn't been finished it may lead to incorrect work of the second process, as a result the data may be damaged.
The solution
We use objects of
Mut
ual
Ex
clusion operating system, in other words
Mutex.
How it works
The Mutex is very simple. We use a principle similar to the principle described in : locking a resource when an object is created, and deallocation of a resource when an object is removed.
Working with Mutex is implemented in two classes.
The first class -
CMutexSync
- represents Mutex handle resource, which is created and deallocated by this class. An object of this class can be global and the one for the whole application.
The second class -
CMutexLock
- is a locked object which awaits for deallocation and locks Mutex handle when created, and deallocates it when removed.
Due to the locking principle of Mutex makes the code nice and readable. It frees the code from WinAPI functions
Example
In this example we will synchronize operation of cycles of two scripts in two charts.
The first script performs some operations for too long (2 seconds in the example). The second script performs its operations in a rapid manner (0.2 second).
Our task is
not to allow new iteration of the rapid script until the first script completes its current iteration
.
Result
Mutex (EURUSD,M1) EURUSD MutexSync created OK! Sleep=2000Mutex (EURUSD,M1) EURUSD lock scopeMutex (EURUSD,M1) EURUSD lock scopeMutex (AUDCAD,H1) AUDCAD MutexSync created OK! Sleep=200Mutex (AUDCAD,H1) AUDCAD lock scopeMutex (EURUSD,M1) EURUSD lock scopeMutex (AUDCAD,H1) AUDCAD lock scopeMutex (EURUSD,M1) EURUSD lock scopeMutex (AUDCAD,H1) AUDCAD lock scopeMutex (EURUSD,M1) EURUSD lock scopeMutex (AUDCAD,H1) AUDCAD lock scopeMutex (EURUSD,M1) EURUSD lock scopeMutex (AUDCAD,H1) AUDCAD lock scopeMutex (EURUSD,M1) EURUSD lock scopeMutex (AUDCAD,H1) AUDCAD lock scopeMutex (EURUSD,M1) EURUSD lock scopeMutex (AUDCAD,H1) AUDCAD lock scopeMutex (EURUSD,M1) EURUSD lock scopeMutex (AUDCAD,H1) AUDCAD lock scopeMutex (EURUSD,M1) EURUSD lock scopeMutex (AUDCAD,H1) AUDCAD lock scope
32/64 Ready
The library works taking into account 32/64 bit count in a similar way to .
I wish you good luck and big profits!