Mutex - Win API
Info
The Mutex - Win API is a Library for MetaTrader 5 that why this is necessaryglobal variables, disk or memory-mapped files, and important operating procedures are no longer an issue. 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 terminalsOr you need to comply with exact operating procedures among different experts of one or different terminals, you should understand the importance of ensuring that performed algorithms do not access the same data via different processes simultaneously.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Library works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Libraries folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use Mutex - Win API
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.
What this tool does
Why this is necessaryGlobal variables, disk or memory-mapped files, and important operating procedures are no longer an issue.
Typical Use Case
This Library excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Library works on MetaTrader 5. Place the file in the MQL5/Libraries folder and restart the terminal.
Description & Settings
Related: cprice - another powerful library for MetaTrader 5 traders.
Why this is necessaryGlobal variables, disk or memory-mapped files, and important operating procedures are no longer an issue!
If you use:
Also recommended: cts logger is a simple and flexible logging system - similar library with strong performance on MetaTrader 5.
- 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
Or you need to comply with exact operating procedures among different experts of one or different terminals, you should understand the importance of ensuring that performed algorithms do not access the same data via different processes simultaneously. This can lead to unexpected effects, such as one process writing while another is reading and making decisions, potentially resulting in data corruption.
The solution
We use objects of mutual exclusion operating system, or mutex.
How it works
The mutex is straightforward. We employ a principle similar to the one described in: locking a resource when an object is created and deallocating it when the object is removed.
Working with mutex is implemented in two classes:
- The first class, CMutexSync, represents the mutex handle resource, which is created and deallocated by this class. An object of this class can be global and used across the entire application.
- The second class, CMutexLock, is a locked object that waits for deallocation and locks the mutex handle when created, and deallocates it upon removal.
The locking principle of mutex simplifies the code and enhances readability, eliminating the need for WinAPI functions.
Example
In this example, we will synchronize the operation of cycles of two scripts in two charts.
The first script performs operations that take too long (2 seconds in the example). The second script executes its operations rapidly (0.2 seconds).
Our goal is to prevent the rapid script from initiating a new iteration until the first script finishes its current iteration.
Result
Mutex (EURUSD,M1) EURUSD MutexSync created ok! Sleep=2000Mutex (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 scope
32/64 ready
The library operates similarly to , considering 32/64-bit count.
Best wishes for success and substantial profits!
You may also like: mt5 to telegram - professional trading notifications library - excellent alternative for library users on MetaTrader 5.
Source Code
#property copyright "robotfx"
#property link "https://robotfx.org"
#property version "1.00"
#include <mqh\lib\mutex.mqh>
#property script_show_inputs
input int sleep=200;
void onstart()
{
sleep(5000); // ,
cmutexsync sync; //
.......
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.