Multithreaded Programming Guide

Exit Print View

Updated: July 2014
 
 

Setting the Mutex's Robust Attribute

pthread_mutexattr_setrobust_np sets the robust attribute of a mutex attribute object.

pthread_mutexattr_setrobust_np Syntax

#include <pthread.h> 
int pthread_mutexattr_setrobust_np(pthread_mutexattr_t *attr, int *robustness);

Note - pthread_mutexattr_setrobust_np() applies only if the symbol _POSIX_THREAD_PRIO_INHERIT is defined.

In the Oracle Solaris 10 and prior releases, the PTHREAD_MUTEX_ROBUST_NP attribute can only be applied to mutexes that are also marked with the PTHREAD_PRIO_INHERIT protocol attribute. This restriction is lifted in subsequent Oracle Solaris releases.


attr points to the mutex attribute object previously created by a call to pthread_mutexattr_init().

robustness defines the behavior when the owner of the mutex terminates without unlocking the mutex, usually because its process terminated abnormally. The value of robustness that is defined in pthread.h is PTHREAD_MUTEX_ROBUST_NP or PTHREAD_MUTEX_STALLED_NP. The default value is PTHREAD_MUTEX_STALLED_NP .

  • PTHREAD_MUTEX_STALLED_NP

    When the owner of the mutex terminates without unlocking the mutex, all subsequent calls to pthread_mutex_lock() are blocked from progress in an unspecified manner.

  • PTHREAD_MUTEX_ROBUST_NP

    When the owner of the mutex terminates without unlocking the mutex, the mutex is unlocked. The next owner of this mutex acquires the mutex with an error return of EOWNERDEAD.


    Note - Your application must always check the return code from pthread_mutex_lock() for a mutex initialized with the PTHREAD_MUTEX_ROBUST_NP attribute.
    • The new owner of this mutex should make the state protected by the mutex consistent. This state might have been left inconsistent when the previous owner terminated.

    • If the new owner is able to make the state consistent, call pthread_mutex_consistent_np() for the mutex before unlocking the mutex. This marks the mutex as consistent and subsequent calls to pthread_mutex_lock() and pthread_mutex_unlock() will behave in the normal manner.

    • If the new owner is not able to make the state consistent, do not call pthread_mutex_consistent_np() for the mutex, but unlock the mutex.

      All waiters are awakened and all subsequent calls to pthread_mutex_lock() fail to acquire the mutex. The return code is ENOTRECOVERABLE. The mutex can be made consistent by calling pthread_mutex_destroy() to uninitialize the mutex, and calling pthread_mutex_int() to reinitialize the mutex. However, the state that was protected by the mutex remains inconsistent and some form of application recovery is required.

    If the thread that acquires the lock with EOWNERDEAD terminates without unlocking the mutex, the next owner acquires the lock with an EOWNERDEAD return code.

pthread_mutexattr_setrobust_np Return Values

On successful completion, pthread_mutexattr_setrobust_np() returns 0. Any other return value indicates that an error occurred.

pthread_mutexattr_setrobust_np() might fail if the following condition occurs:

EINVAL

Description: The value specified by attr or robustness is invalid.