ptg10805159
Section 12.4 Synchronization Attributes 433
#include <pthread.h>
int pthread_mutex_consistent(pthread_mutex_t *mutex);
Returns: 0 if OK, error number on failure
If a thread unlocks a mutex without first callingpthread_mutex_consistent,
then other threads that areblocked while trying to acquirethe mutex will see error
returns ofENOTRECOVERABLE.Ifthis happens, the mutex is no longer usable. By
callingpthread_mutex_consistent beforehand, a thread allows the mutex to
behave normally, so it can continue to be used.
Thetypemutex attribute controls the locking characteristics of the mutex. POSIX.1
defines four types:
PTHREAD_MUTEX_NORMAL Astandardmutex type that doesn’t do any
special error checking or deadlock detection.
PTHREAD_MUTEX_ERRORCHECK Amutex type that provides error checking.
PTHREAD_MUTEX_RECURSIVE Amutex type that allows the same thread to lock
it multiple times without first unlocking it. A
recursive mutex maintains a lock count and isn’t
released until it is unlocked the same number of
times it is locked. Thus, if you lock a recursive
mutex twice and then unlock it, the mutex
remains locked until it is unlocked a second time.
PTHREAD_MUTEX_DEFAULT Amutex type providing default characteristics
and behavior.Implementations arefree to map it
to one of the other mutex types. For example,
Linux 3.2.0 maps this type to the normal mutex
type, whereas FreeBSD 8.0 maps it to the error-
checking type.
The behavior of the four types is summarized in Figure12.5. The‘‘Unlock when
not owned’’column refers to one thread unlocking a mutex that was locked by a
different thread. The‘‘Unlock when unlocked’’column refers to what happens when a
thread unlocks a mutex that is already unlocked, which usually is a coding mistake.
Mutex type Relock without unlock? Unlock when not owned? Unlock when unlocked?
PTHREAD_MUTEX_NORMAL deadlock undefined undefined
PTHREAD_MUTEX_ERRORCHECK returns error returns error returns error
PTHREAD_MUTEX_RECURSIVE allowed returns error returns error
PTHREAD_MUTEX_DEFAULT undefined undefined undefined
Figure 12.5Mutex type behavior