File Lease And Lock Manager
Several terminals or EA instances sharing one data file on disk (a shared settings file, a cross-EA signal file, a common-folder log) need some way to avoid stepping on each other mid-write. A plain "does the lock file exist" check solves the easy case but has an ugly failure mode: if the terminal holding the lock crashes, gets its process killed, or the EA is simply removed from the chart before it releases, the lock file is left behind forever - and every other terminal is now permanently locked out of a resource nothing is actually using anymore. This library's lock file is a lease, not a permanent claim: it stores the current owner's id and the time it last renewed the lease. TryAcquire() succeeds when the file does not exist, when the caller already owns it (refreshing the timestamp), or when a DIFFERENT owner's lease has gone older than a caller-chosen TTL - at which point it is treated as abandoned and is stolen outright. A legitimate long-running holder simply calls TryAcquire() again periodically (a heartbeat) to keep renewing its own lease before the TTL expires; anything that stops doing that, for any reason including a crash, naturally loses the lock to the next caller within one TTL window rather than locking the resource forever. Release() only releases a lease the caller still actually owns - if a lease was stolen out from under a caller (it stopped renewing for too long and someone else took over), a late Release() call from the original owner does nothing rather than deleting a lock file that now belongs to someone else, which would otherwise let a straggler yank the lock away from its legitimate new holder. Notes and limitations - This is ADVISORY locking. It only stops a caller that also calls TryAcquire() first - it cannot prevent a program that ignores this header entirely from editing the shared file directly, the same limitation every advisory file lock has on any platform. - The demo drives an explicit simulated clock (a datetime parameter, not TimeLocal()/TimeCurrent() read internally) so the TTL/staleness behaviour is deterministic and reproducible on every run - real callers pass TimeLocal() (or their own clock source) instead. - An unreadable/corrupt lock file (e.g. damaged by a genuinely torn write, not just a normal crash) is treated the same as a stale one - it can be stolen - since a record this header cannot even parse cannot be trusted to represent a live holder either. - Owner ids are compared as plain strings - use something that genuinely identifies the caller uniquely (chart id, magic number, process-specific string), not a constant, or every instance of the same EA will treat every other instance's lease as its own to renew. - There is no queuing or fairness guarantee - if two callers race to steal the same stale lease, whichever calls TryAcquire() last wins (its write is the one left on disk), the same "last writer wins" behaviour any lock file scheme without a proper OS-level advisory lock primitive has.
Script · MT4 · v1.0 · Free
About File Lease And Lock Manager
File Lease And Lock Manager is a script for MetaTrader 4. It has 1 setting, such as Ttl seconds. MQL source code included.
Files in the download: FileLeaseLockManager.mqh, FileLeaseLockManager.txt, FileLeaseLockManagerDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Ttl seconds | 30 | Lease lifetime before a lock is considered stale |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Buy/Sell Arrows · MT4 Script · MQL4 Source Code