Loading…

Concurrency Torture Kit

Any tool that shares state between two chart instances through a file in MQL4\Files or a GlobalVariableSet/Get value is exposed to a race condition the moment two instances touch that state close together in time - a read, then a modify, then a write is not one atomic step, and MQL4 gives no file lock or transaction to make it one. Most published tools that use shared files or globals for coordination have never actually been tested under contention, because MQL4 has no threads and launching two real chart instances synchronised to the millisecond by hand is impractical. This library reproduces the exact failure without needing two real processes. Each race opens two independent handles (or issues two independent GlobalVariable calls) to the SAME shared state and interleaves the read/write steps by hand in the precise order two genuinely concurrent instances would race them - both sides read the same stale value before either writes back, so every round loses exactly one of the two increments attempted. The naive path's final count is checked mechanically against the expected total, not just eyeballed, so "the race happened" is a verified fact rather than a claim. The hardened path fixes it two different ways because the two kinds of shared state have different tools available. The shared file has no native compare-and-set, so it gets a hand-built one: a nonce written alongside the counter, re-checked immediately before every commit, with a retry loop that re-reads and tries again on a mismatch. The shared global variable has a genuine built-in compare-and-set in GlobalVariableSetOnCondition(), so the hardened path uses that directly instead of reinventing it. Both hardened paths converge to the correct total every time, with the retry count reported explicitly so it is clear the mechanism did real work rather than the totals matching by coincidence. Notes and limitations - This is a single-process simulation of contention, not a literal multi-process test. It reproduces the exact primitives and the exact harmful interleaving a real second process would cause, and the file and global-variable APIs exercised are identical to what a real second chart instance would call, but it does not prove the terminal behaves identically when two OS-level operations land in the same microsecond. Treat it as a rigorous demonstration of the failure mode and the fix, not a substitute for real multi-instance soak testing on a specific broker/terminal build. - The hardened file path's nonce lives alongside the counter in the same 4+4-byte record; it is not a general-purpose lock file usable by unrelated tools without adapting the layout. - ConcurrencyTortureKitRaceOnGlobal/HardenedOnGlobal both reset the target global variable to 0 at the start of the call - do not point either at a global variable already carrying state you need to keep. - The demo deletes its own global variable at the end of the run so it leaves nothing persistent behind; the sandbox counter file is left in MQL4\Files for inspection and can be deleted manually. - No trading functions are used anywhere in this library.

Script · MT5 · v1.0 · Free

About Concurrency Torture Kit

Concurrency Torture Kit is a script for MetaTrader 5. It has 2 settings, such as Iterations and Max retries. MQL source code included.

Files in the download: ConcurrencyTortureKit.mq5.mqh, ConcurrencyTortureKit.txt, ConcurrencyTortureKitDemo.ex5, ConcurrencyTortureKitDemo.mq5.

Settings

SettingDefaultWhat it does
Iterations200race rounds to run per test
Max retries50retry cap for the hardened variants

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria