thread.mutex

March 26, 2020 Contributors

Name

thread.mutex — create a new mutex

Synopsis

thread.mutex(type);

type: integer, optional

Description

**Configuration Change. ** This feature is available as of version 3.2.

Create a new mutex for coordinating access to critical sections. The optional parameter may be one of the following:

  • thread.MUTEX_NORMAL – this is the default value

  • thread.MUTEX_RECURSIVE – define a recursive lock. Returns a mutex object that supports "lock", "unlock" and "trylock" methods.

  • thread.MUTEX_ERRORCHECK – define an error-checking lock. Returns a mutex object that supports "lock", "unlock" and "trylock" methods.

This function returns a mutex object.

Example code follows:

counter_lock = thread.mutex()
counter = 0;

function counter_inc()
   counter_lock:lock()
   counter = counter + 1
   counter_lock:unlock()
end

function counter_get()
   local value
   counter_lock:lock()
   value = counter
   counter_lock:unlock()
   return value
end

-- Incrementing the counter will now have consistent results regardless of
-- which threads call this function
counter_inc()
print("value is", counter_get())

Enable this function with the statement require('thread');.

See Also

msys.runInPool, msys.sleep, msys.lock, msys.unlock