Mutex

    Available since v0.4.0

    Lock Concurrent Operations

    local mutex = require "mutex"
    

    mutex()

    The mutex package provides a single function, mutex(), that will create a new mutex-based lock. The returned function can be called with a function argument to synchronize concurrent operations on a shared resource.

    local mutex = require "mutex"
    local lock = mutex()
    
    local res = lock(function()
      -- Critical section
      -- This code will be concurrency-safe
      return do_thing_with_resource() -- Result will be returned to the outer scope `res`
    end)
    

    NOTE: Beware of recursive locking (calling lock() again inside the lock's critical section). The code will deadlock.