๐ Locker Guide
July 25, 2025 ยท View on GitHub
๐ Quick Start Guide for file locking and process concurrency with LuaDoTheWorld
๐ What You'll Learn
- โ How to lock and unlock files
- โ How to manage concurrency between processes
- โ How to use lockers in forked processes
๐ ๏ธ Prerequisites
- LuaDoTheWorld installed and required in your script
๐ Lock and Unlock a File
local dtw = require("luaDoTheWorld/luaDoTheWorld")
local locker = dtw.newLocker()
locker.lock("a.txt")
-- Do something with the locked file
locker.unlock("a.txt")
๐ค Locking in Multiple Processes (Fork Example)
Create 30 forks, each writing its number to a file safely using a locker:
local dtw = require("luaDoTheWorld/luaDoTheWorld")
local all_forks = {}
local total_forks = 30
dtw.remove_any("a.txt")
for i = 1, total_forks do
local fork = dtw.newFork(function()
local locker = dtw.newLocker()
locker.lock("a.txt")
local old = dtw.load_file("a.txt")
if old == nil then old = "" end
old = old .. i .. "\n"
dtw.write_file("a.txt", old)
locker.unlock("a.txt")
end)
all_forks[i] = fork
end
-- Wait for all child processes to finish
for i = 1, total_forks do
all_forks[i].wait(-1)
end
๐ Quick Reference
| Function | What it does | Example |
|---|---|---|
dtw.newLocker() | Create a new locker | local locker = dtw.newLocker() |
locker.lock(path) | Lock a file | locker.lock("a.txt") |
locker.unlock(path) | Unlock a file | locker.unlock("a.txt") |
dtw.newFork(fn) | Create a new process | dtw.newFork(function() ... end) |
๐ Notes
- Lockers prevent file access conflicts between processes (not threads).
- Only works if all processes use LuaDoTheWorld's locker system.
๐ Need Help?
- ๐ Check the main SDK documentation
- ๐ Look at other example scripts in the SDK
- ๐ Report issues on our GitHub repository