Skip to content

Latest commit

Β 

History

History
98 lines (67 loc) Β· 2.38 KB

File metadata and controls

98 lines (67 loc) Β· 2.38 KB

πŸ”’ Locker Guide

Lua API Difficulty Version

πŸš€ 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

Footer