Initialises or destroys a mutex.
Threads Library (libpthreads.a)
#include <pthread.h>int pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
The pthread_mutex_init function initialises the mutex referenced by mutex with attributes specified by attr. If attr is NULL, the default mutex attributes are used; the effect is the same as passing the address of a default mutex attributes object. Upon successful initialisation, the state of the mutex becomes initialised and unlocked.
Attempting to initialise an already initialised mutex results in undefined behaviour.
The pthread_mutex_destroy function destroys the mutex object referenced by mutex; the mutex object becomes, in effect, uninitialised. An implementation may cause pthread_mutex_destroy to set the object referenced by mutex to an invalid value. A destroyed mutex object can be re-initialised using pthread_mutex_init; the results of otherwise referencing the object after it has been destroyed are undefined.
It is safe to destroy an initialised mutex that is unlocked. Attempting to destroy a locked mutex results in undefined behaviour.
In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialise mutexes that are statically allocated. The effect is equivalent to dynamic initialisation by a call to pthread_mutex_init with parameter attr specified as NULL, except that no error checks are performed.
|mutex||Specifies the mutex to delete.|
If successful, the pthread_mutex_init and pthread_mutex_destroy functions return zero. Otherwise, an error number is returned to indicate the error. The EBUSY and EINVAL error checks act as if they were performed immediately at the beginning of processing for the function and cause an error return prior to modifying the state of the mutex specified by mutex.
The pthread_mutex_init function will fail if:
|ENOMEM||Insufficient memory exists to initialise the mutex.|
|EINVAL||The value specified by attr is invalid.|
The pthread_mutex_destroy function will fail if:
|EBUSY||The implementation has detected an attempt to destroy the object referenced by mutex while it is locked or referenced (for example, while being used in a pthread_cond_waitor pthread_cond_timedwait by another thread.|
|EINVAL||The value specified by mutex is invalid.|
These functions will not return an error code of EINTR.
This subroutine is part of the Base Operating System (BOS) Runtime.
The pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_trylock, pthread_mutexattr_setpshared subroutines, the pthread.h file.