The
pthread_mutex_init() function creates a new mutex, with attributes specified with
attr. If
attr is
NULL, the default attributes are used.
The macro
PTHREAD_MUTEX_INITIALIZER can be used to initialize a mutex when the default attributes are appropriate and the mutex can be statically allocated. The behavior is similar to
pthread_mutex_init() with
attr specified as
NULL, except that no error checking is done.
The
pthread_mutex_destroy() function frees the resources allocated for
mutex. It is possible to reinitialize a destroyed mutex, but undefined behavior may follow if the destroyed object is otherwise referenced.
The
pthread_mutex_lock() function locks
mutex. If the mutex is already locked, the calling thread will block until the mutex becomes available. The error conditions may vary depending on the type of the mutex; see
pthread_mutexattr(3) for additional details.
The
pthread_mutex_trylock() function locks
mutex. If the mutex is already locked,
pthread_mutex_trylock() will not block waiting for the mutex, but will return an error condition.
The
pthread_mutex_unlock() function unlocks an acquired
mutex. When operating with the default mutex type, undefined behavior follows if a thread tries to unlock a mutex that has not been locked by it, or if a thread tries to release a mutex that is already unlocked.