ptg10805159
432 Thread Control Chapter 12
restrict the moreexpensive implementation to the case in which mutexes areshared
among processes.
Therobustmutex attribute is related to mutexes that areshared among multiple
processes. It is meant to address the problem of mutex state recovery when a process
terminates while holding a mutex. When this happens, the mutex is left in a locked
state and recovery is difficult. Threads blocked on the lock in other processes will block
indefinitely.
We can use thepthread_mutexattr_getrobustfunction to get the value of the
robustmutex attribute. To set the value of therobustmutex attribute, we can call the
pthread_mutexattr_setrobustfunction.
#include <pthread.h>
int pthread_mutexattr_getrobust(const pthread_mutexattr_t *
restrictattr,
int *restrictrobust);
int pthread_mutexattr_setrobust(pthread_mutexattr_t *attr,
introbust);
Both return: 0 if OK, error number on failure
Thereare two possible values for the robust attribute. The default is
PTHREAD_MUTEX_STALLED,which means that no special action is taken when a
process terminates while holding a mutex. In this case, use of the mutex can result in
undefined behavior,and applications waiting for it to be unlocked areeffectively
‘‘stalled.’’The other value isPTHREAD_MUTEX_ROBUST.This value will cause a thread
blocked in a call topthread_mutex_lockto acquirethe lock when another process
holding the lock terminates without first unlocking it, but the return value from
pthread_mutex_lockisEOWNERDEADinstead of 0. Applications can use this special
return value as an indication that they need to recover whatever state the mutex was
protecting, if possible (the details of what state is being protected and how it can be
recovered will vary among applications). Note that theEOWNERDEADerror return isn’t
really an error in this case, because the caller will own the lock.
Using robust mutexes changes the way we usepthread_mutex_lock,because we
now have to check for three return values instead of two: success with no recovery
needed, success but recovery needed, and failure. However, if we don’t use robust
mutexes, then we can continue to check only for success and failure.
Of the four platforms covered in this text, only Linux 3.2.0 currently supports robust pthread
mutexes. Solaris 10 supports robust mutexes only in its Solaris threads library (see the
mutex_init(3C)Solaris manual page for moreinformation). However, in Solaris 11, robust
pthread mutexes aresupported.
If the application state can’t be recovered, the mutex will be in a permanently
unusable state after the thread unlocks the mutex.To prevent this problem, the thread
can call the pthread_mutex_consistent function to indicate that the state
associated with the mutex is consistent beforeunlocking the mutex.