To have better control of resources and how threads access them, the GNU C Library implements a mutex object, which can help avoid race conditions and other concurrency issues. The term “mutex” refers to mutual exclusion.
The fundamental data type for a mutex is the mtx_t:
The ISO C standard defines several types of mutexes. They are represented by the following symbolic constants:
mtx_plainmtx_recursivemtx_timedThe following functions are used for working with mutexes:
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
mtx_initcreates a new mutex object with type type. The object pointed to by mutex is set to the identifier of the newly created mutex.Not all combinations of mutex types are valid for the
typeargument. Valid uses of mutex types for thetypeargument are:
mtx_plain- A non-recursive mutex that does not support timeout.
mtx_timed- A non-recursive mutex that does support timeout.
mtx_plain | mtx_recursive- A recursive mutex that does not support timeout.
mtx_timed | mtx_recursive- A recursive mutex that does support timeout.
This function returns either
thrd_successorthrd_error.
Preliminary: | MT-Safe | AS-Unsafe lock | AC-Unsafe lock | See POSIX Safety Concepts.
mtx_lockblocks the current thread until the mutex pointed to by mutex is locked. The behavior is undefined if the current thread has already locked the mutex and the mutex is not recursive.Prior calls to
mtx_unlockon the same mutex synchronize-with this operation (if this operation succeeds), and all lock/unlock operations on any given mutex form a single total order (similar to the modification order of an atomic).This function returns either
thrd_successorthrd_error.
Preliminary: | MT-Safe | AS-Unsafe lock | AC-Unsafe lock | See POSIX Safety Concepts.
mtx_timedlockblocks the current thread until the mutex pointed to by mutex is locked or until the calendar time pointed to by time_point has been reached. Since this function takes an absolute time, if a duration is required, the calendar time must be calculated manually. See Time Basics, and Calendar Time.If the current thread has already locked the mutex and the mutex is not recursive, or if the mutex does not support timeout, the behavior of this function is undefined.
Prior calls to
mtx_unlockon the same mutex synchronize-with this operation (if this operation succeeds), and all lock/unlock operations on any given mutex form a single total order (similar to the modification order of an atomic).This function returns either
thrd_successorthrd_error.
Preliminary: | MT-Safe | AS-Unsafe lock | AC-Unsafe lock | See POSIX Safety Concepts.
mtx_trylocktries to lock the mutex pointed to by mutex without blocking. It returns immediately if the mutex is already locked.Prior calls to
mtx_unlockon the same mutex synchronize-with this operation (if this operation succeeds), and all lock/unlock operations on any given mutex form a single total order (similar to the modification order of an atomic).This function returns
thrd_successif the lock was obtained,thrd_busyif the mutex is already locked, andthrd_erroron failure.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
mtx_unlockunlocks the mutex pointed to by mutex. The behavior is undefined if the mutex is not locked by the calling thread.This function synchronizes-with subsequent
mtx_lock,mtx_trylock, andmtx_timedlockcalls on the same mutex. All lock/unlock operations on any given mutex form a single total order (similar to the modification order of an atomic).This function returns either
thrd_successorthrd_error.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
mtx_destroydestroys the mutex pointed to by mutex. If there are any threads waiting on the mutex, the behavior is undefined.