π Quick Start Guide for file locking and process concurrency with LuaDoTheWorld
- β How to lock and unlock files
- β How to manage concurrency between processes
- β How to use lockers in forked processes
- LuaDoTheWorld installed and required in your script
local dtw = require("luaDoTheWorld/luaDoTheWorld")
local locker = dtw.newLocker()
locker.lock("a.txt")
-- Do something with the locked file
locker.unlock("a.txt")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| 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) |
- Lockers prevent file access conflicts between processes (not threads).
- Only works if all processes use LuaDoTheWorld's locker system.
- π Check the main SDK documentation
- π Look at other example scripts in the SDK
- π Report issues on our GitHub repository