pthread_mutex_init(mutex, attr)
pthread_mutexattr_destroy(attr)
Mutex variables must be declared with type pthread_mutex_t
, and must be initialized before they can be used. There are two ways to initialize a mutex variable:
Statically, when it is declared. For example:
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
Dinamically, using pthread_mutex_init()
. For example:
pthread_mutex_t mymutex;
pthread_mutex_init(&mymutex, NULL);
The mutex is initially unlocked.
The attr
object is used to establish properties for the mutex object, and must be of type pthread_mutexattr_t
if used (may be specified as NULL to accept defaults). The Pthreads standard defines three optional mutex attributes:
Note that not all implementations may provide the three optional mutex attributes.
The pthread_mutexattr_init()
and pthread_mutexattr_destroy()
routines are used to create and destroy mutex attribute objects respectively.
pthread_mutex_destroy()
should be used to free a mutex object which is no longer needed.