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;
Dynamically, with the
pthread_mutex_init() routine. This method permits setting mutex object attributes, attr.
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.
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.