Base class: Object
A mutex ("mutually exclusive") is used to define a block of code that two threads may not execute at the same time. Use of mutexes should be minimized, as they can slow down multi-threaded code so that it loses its advantages over single-threaded code.
Name | Type | Description |
---|---|---|
Lock | Method | Locks the mutex |
Unlock | Method | Unlocks the mutex |
CreateMutex | Function | Creates a new mutex |
-- Create a new mutex
local mutex = CreateMutex()
-- Thread 1
mutex:Lock()
-- Code that should not be executed by both threads at the same time
mutex:Unlock()
-- Thread 2
mutex:Lock()
-- Code that should not be executed by both threads at the same time
mutex:Unlock()
Note: The example assumes that multi-threading is already set up in Lua.