Multithreaded Programming Guide

Mutual Exclusion Lock Attributes

Use mutual exclusion locks (mutexes) to serialize thread execution. Mutual exclusion locks synchronize threads, usually by ensuring that only one thread at a time executes a critical section of code. Mutex locks can also preserve single-threaded code.

To change the default mutex attributes, you can declare and initialize an attribute object. Often, the mutex attributes are set in one place at the beginning of the application so they can be located quickly and modified easily. Table 4-1 lists the functions discussed in this section that manipulate mutex attributes.

Table 4-1 Mutex Attributes Routines

Operation 

Destination Discussion 

 

Initialize a mutex mttribute object 

"pthread_mutexattr_init(3THR)"

Destroy a mutex attribute object 

"pthread_mutexattr_destroy(3THR)"

Set the scope of a mutex 

"pthread_mutexattr_setpshared(3THR)"

Get the scope of a mutex 

"pthread_mutexattr_getpshared(3THR)"

Set the mutex type attribute 

"pthread_mutexattr_settype(3THR)"

Get the mutex type attribute 

"pthread_mutexattr_gettype(3THR)"

Set mutex attribute's protocol 

"pthread_mutexattr_setprotocol(3T)"

Get mutex attribute's protocol 

"pthread_mutexattr_getprotocol(3T)"

Set mutex attribute's priority ceiling 

"pthread_mutexattr_setprioceiling(3T)"

Get mutex attribute's priority ceiling 

"pthread_mutexattr_getprioceiling(3T)"

Set mutex's priority ceiling 

"pthread_mutex_setprioceiling(3T)"

Get mutex's priority ceiling 

"pthread_mutex_getprioceiling(3T)"

Set mutex's robust attribute 

"pthread_mutexattr_setrobust_np(3T)"

Get mutex's robust attribute 

"pthread_mutexattr_getrobust_np(3T)"

The differences between Solaris threads and POSIX threads, when defining the scope of a mutex, are shown in Table 4-2.

Table 4-2 Mutex Scope Comparison

Solaris 

POSIX 

Definition 

USYNC_PROCESS

PTHREAD_PROCESS_SHARED

Use to synchronize threads in this and other processes 

USYNC_PROCESS_ROBUST

No POSIX equivalent 

Use to robustly synchronize threads between processes

USYNC_THREAD

PTHREAD_PROCESS_PRIVATE

Use to synchronize threads in this process only 

Initialize a Mutex Attribute Object

pthread_mutexattr_init(3THR)

Use pthread_mutexattr_init(3THR) to initialize attributes associated with this object to their default values. Storage for each attribute object is allocated by the threads system during execution.

The default value of the pshared attribute when this function is called is PTHREAD_PROCESS_PRIVATE, which means that the initialized mutex can be used within a process.

Prototype:
int	pthread_mutexattr_init(pthread_mutexattr_t *mattr);
#include <pthread.h>

pthread_mutexattr_t mattr;
int ret;

/* initialize an attribute to default value */
ret = pthread_mutexattr_init(&mattr); 

mattr is an opaque type that contains a system-allocated attribute object. The possible values of mattr's scope are PTHREAD_PROCESS_PRIVATE (the default) and PTHREAD_PROCESS_SHARED.

Before a mutex attribute object can be reinitialized, it must first be destroyed by a call to pthread_mutexattr_destroy(3T). The pthread_mutexattr_init() call results in the allocation of an opaque object. If the object is not destroyed, a memory leak will result.

Return Values

Returns zero after completing successfully. Any other returned value indicates that an error occurred. If either of the following conditions occurs, the function fails and returns the corresponding value.


ENOMEM

There is not enough memory to initialize the mutex attributes object.

Destroy a Mutex Attribute Object

pthread_mutexattr_destroy(3THR)

pthread_mutexattr_destroy(3THR) deallocates the storage space used to maintain the attribute object created by pthread_mutexattr_init().

Prototype:
int	pthread_mutexattr_destroy(pthread_mutexattr_t *mattr)
#include <pthread.h>

pthread_mutexattr_t mattr;
int ret;

/* destroy an attribute */
ret = pthread_mutexattr_destroy(&mattr); 

Return Values

pthread_mutexattr_destroy() returns zero after completing successfully. Any other returned value indicates that an error occurred. If the following condition occurs, the function fails and returns the corresponding value.


EINVAL

The value specified by mattr is invalid.

Set the Scope of a Mutex

pthread_mutexattr_setpshared(3THR)

pthread_mutexattr_setpshared(3THR) sets the scope of the mutex variable.

The scope of a mutex variable can be either process private (intraprocess) or system wide (interprocess). If the mutex is created with the pshared attribute set to the PTHREAD_PROCESS_SHARED state, and it exists in shared memory, it can be shared among threads from more than one process. This is equivalent to the USYNC_PROCESS flag in mutex_init() in the original Solaris threads.

Prototype:
int	pthread_mutexattr_setpshared(pthread_mutexattr_t *mattr,
    int pshared);
#include <pthread.h>

pthread_mutexattr_t mattr;
int ret;

ret = pthread_mutexattr_init(&mattr);
/*
 * resetting to its default value: private
 */
ret = pthread_mutexattr_setpshared(&mattr,
     PTHREAD_PROCESS_PRIVATE);

If the mutex pshared attribute is set to PTHREAD_PROCESS_PRIVATE, only those threads created by the same process can operate on the mutex.

Return Values

Returns zero after completing successfully. Any other returned value indicates that an error occurred. If the following condition occurs, the function fails and returns the corresponding value.


EINVAL

The value specified by mattr is invalid.

Get the Scope of a Mutex

pthread_mutexattr_getpshared(3THR)

pthread_mutexattr_getpshared(3THR) returns the scope of the mutex variable defined by pthread_mutexattr_setpshared().

Prototype:
int	pthread_mutexattr_getpshared(pthread_mutexattr_t *mattr,
    int *pshared);
#include <pthread.h>

pthread_mutexattr_t mattr;
int pshared, ret;

/* get pshared of mutex */
ret = pthread_mutexattr_getpshared(&mattr, &pshared); 

Get the current value of pshared for the attribute object mattr. It is either PTHREAD_PROCESS_SHARED or PTHREAD_PROCESS_PRIVATE.

Return Values

Returns zero after completing successfully. Any other returned value indicates that an error occurred. If the following condition occurs, the function fails and returns the corresponding value.


EINVAL

The value specified by mattr is invalid.

Set the Mutex Type Attribute

pthread_mutexattr_settype(3THR)

#include <pthread.h>

int pthread_mutexattr_settype(pthread_mutexattr_t  *attr , int type);

pthread_mutexattr_settype(3THR) sets the mutex type attribute. The default value of the type attribute is PTHREAD_MUTEX_DEFAULT.

The type argument specifies the type of mutex. Valid mutex types include:


PTHREAD_MUTEX_NORMAL

This type of mutex does not detect deadlock. A thread attempting to relock this mutex without first unlocking it will deadlock. Attempting to unlock a mutex locked by a different thread results in undefined behavior. Attempting to unlock an unlocked mutex results in undefined behavior.


PTHREAD_MUTEX_ERRORCHECK

This type of mutex provides error checking. A thread attempting to relock this mutex without first unlocking it will return with an error. A thread attempting to unlock a mutex which another thread has locked will return with an error. A thread attempting to unlock an unlocked mutex will return with an error.


PTHREAD_MUTEX_RECURSIVE

A thread attempting to relock this mutex without first unlocking it will succeed in locking the mutex. The relocking deadlock which can occur with mutexes of type PTHREAD_MUTEX_NORMAL cannot occur with this type of mutex. Multiple locks of this mutex require the same number of unlocks to release the mutex before another thread can acquire the mutex. A thread attempting to unlock a mutex which another thread has locked will return with an error. A thread attempting to unlock an unloc ked mutex will return with an error. This type of mutex is only supported for mutexes whose process shared attribute is PTHREAD_PROCESS_PRIVATE.


PTHREAD_MUTEX_DEFAULT

Attempting to recursively lock a mutex of this type results in undefined behaviour. Attempting to unlock a mutex of this type which was not locked by the calling thread results in undefined behaviour. Attempting to unlock a mutex of this type which is not locked results in undefined behaviour. An implementation is allowed to map this mutex to one of the other mutex types. (For Solaris threads, PTHREAD_PROCESS_DEFAULT is mapped to PTHREAD_PROCESS_NORMAL.)

Return Values

If successful, the pthread_mutexattr_settype function returns zero. Otherwise, an error number is returned to indicate the error.


EINVAL

The value type is invalid.


EINVAL

The value specified by attr is invalid.

Get the Mutex Type Attribute

pthread_mutexattr_gettype(3THR)

#include <pthread.h>

int pthread_mutexattr_gettype(pthread_mutexattr_t  *attr , int  *type);

pthread_mutexattr_gettype(3THR) gets the mutex type attribute set by pthread_mutexattr_settype(). The default value of the type attribute is PTHREAD_MUTEX_DEFAULT.

The type argument specifies the type of mutex. Valid mutex types include:

For a description of each type, see "pthread_mutexattr_settype(3THR)".

Set Mutex Attribute's Protocol

pthread_mutexattr_setprotocol(3T)

pthread_mutexattr_setprotocol(3T) sets the protocol attribute of a mutex attribute object.

#include <pthread.h>

int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol);

attr points to a mutex attribute object created by an earlier call to pthread_mutexattr_init().

protocol defines the protocol applied to the mutex attribute object.

The value of protocol, defined in pthread.h, can be: PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT.

When a thread owns a mutex that is intialized with PTHREAD_PRIO_INHERIT or PTHREAD_PRIO_PROTECT, and that thread's original priority changes, such as by a call to sched_setparam(), the scheduler does not move the thread to the tail of the scheduling queue at it's new priority. Similarly, when a thread unlocks a mutex that is intialized with PTHREAD_PRIO_INHERIT or PTHREAD_PRIO_PROTECT, and that thread's original priority changes, the scheduler does not move the thread to the tail of the scheduling queue at it's new priority.

If a thread simultaneously owns several mutexes initialized with a mix of PTHREAD_PRIO_INHERIT and PTHREAD_PRIO_PROTECT, it executes at the highest priority obtained by either of these protocols.

Return Values

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

If either of the following conditions occurs, pthread_mutexattr_setprotocol() fails and returns the corresponding value.


ENOSYS

Neither of the options _POSIX_THREAD_PRIO_INHERIT and _POSIX_THREAD_PRIO_PROTECT is defined and the implementation does not support the function.


ENOTSUP

The value specified by protocol is an unsupported value.

If either of the following conditions occurs, pthread_mutexattr_setprotocol() might fail and return the corresponding value.


EINVAL

The value specified by attr or protocol is not valid.


EPERM

The caller does not have the privilege to perform the operation.

Get Mutex Attribute's Protocol

pthread_mutexattr_getprotocol(3T)

pthread_mutexattr_getprotocol(3T) gets the protocol attribute of a mutex attribute object.

#include <pthread.h>

int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol);

attr points to a mutex attribute object created by an earlier call to pthread_mutexattr_init().

protocol contains the protocol attribute: PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, or PTHREAD_PRIO_PROTECT.

Return Values

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

If the following condition occurs, pthread_mutexattr_getprotocol() fails and returns the corresponding value.


ENOSYS

Neither of the options, _POSIX_THREAD_PRIO_INHERIT nor _POSIX_THREAD_PRIO_PROTECT is defined and the implementation does not support the function.

If either of the following conditions occurs, pthread_mutexattr_getprotocol() might fail and return the corresponding value.


EINVAL

The value specified by attr is invalid.


EPERM

The caller does not have the privilege to perform the operation.

Set Mutex Attribute's Priority Ceiling

pthread_mutexattr_setprioceiling(3T)

pthread_mutexattr_setprioceiling(3T) sets the priority ceiling attribute of a mutex attribute object.

#include <pthread.h>

int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling, int *oldceiling);

attr points to a mutex attribute object created by an earlier call to pthread_mutexattr_init().


Note -

The attr mutex attribute object includes the priority ceiing attribute only if the symbol _POSIX_THREAD_PRIO_PROTECT is defined.


prioceiling specifies the priority ceiling of initialized mutexes. The ceiling defines the minimum priority level at which the critical section guarded by the mutex is executed. prioceiling will be within the maximum range of priorities defined by SCHED_FIFO. To avoid priority inversion, prioceiling will be set to a priority higher than or equal to the highest priority of all the threads that might lock the particular mutex.

oldceiling contains the old priority ceiling value.

Return Values

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

If any of the following conditions occurs, pthread_mutexattr_setprioceiling() fails and returns the corresponding value.


ENOSYS

The option _POSIX_THREAD_PRIO_PROTECT is not defined and the implementation does not support the function.

If either of the following conditions occurs, pthread_mutexattr_setprioceiling() might fail and return the corresponding value.


EINVAL

The value specified by attr or prioceiling is invalid.


EPERM

The caller does not have the privilege to perform the operation.

Get Mutex Attribute's Priority Ceiling

pthread_mutexattr_getprioceiling(3T)

pthread_mutexattr_getprioceiling(3T) gets the priority ceiling attribute of a mutex attribute object.

#include <pthread.h>

int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling);

attr designates the attribute object created by an earlier call to pthread_mutexattr_init().


Note -

The attr mutex attribute object includes the priority ceiing attribute only if the symbol _POSIX_THREAD_PRIO_PROTECT is defined.


pthread_mutexattr_getprioceiling() returns the priority ceiling of initialized mutexes in prioceiling. The ceiling defines the minimum priority level at which the critical section guarded by the mutex is executed. prioceiling will be within the maximum range of priorities defined by SCHED_FIFO. To avoid priority inversion, prioceiling will be set to a priority higher than or equal to the highest priority of all the threads that might lock the particular mutex.

Return Values

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

If any of the following conditions occurs, pthread_mutexattr_getprioceiling() fails and returns the corresponding value.


ENOSYS

The option _POSIX_THREAD_PRIO_PROTECT is not defined and the implementation does not support the function.

If either of the following conditions occurs, pthread_mutexattr_getprioceiling() might fail and return the corresponding value.


EINVAL

The value specified by attr is invalid.


EPERM

The caller does not have the privilege to perform the operation.

Set Mutex's Priority Ceiling

pthread_mutex_setprioceiling(3T)

pthread_mutex_setprioceiling(3T) sets the priority ceiling of a mutex.

#include <pthread.h>

int pthread_mutex_setprioceiling(pthread_mutexatt_t *mutex, int prioceiling, int *old_ceiling);

pthread_mutex_setprioceiling() changes the priority ceiling, prioceiling, of a mutex, mutex. pthread_mutex_setprioceiling() locks the mutex if it is unlocked, or blocks until it can successfully lock the mutex, changes the priority celing of the mutex and releases the mutex. The process of locking the mutex need not adhere to the priority protect protocol.


Note -

The mutex attribute object, mutex, includes the priority ceiing attribute only if the symbol _POSIX_THREAD_PRIO_PROTECT is defined.


If pthread_mutex_setprioceiling() succeeds, the previous value of the priority ceiling is returned in old_ceiling. If pthread_mutex_setprioceiling() fails, the mutex priority ceiling remains unchanged.

Return Values

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

If the following condition occurs, pthread_mutexatt_setprioceiling() fails and returns the corresponding value.


ENOSYS

The option _POSIX_THREAD_PRIO_PROTECT is not defined and the implementation does not support the function.

If any of the following conditions occurs, pthread_mutex_setprioceiling() might fail and return the corresponding value.


EINVAL

The priority requested by prioceiling is out of range.


EINVAL

The value specified by mutex does not refer to a currently existing mutex.


ENOSYS

The implementation does not support the priority ceiling protocol for mutexes.


EPERM

The caller does not have the privilege to perform the operation.

Get Mutex's Priority Ceiling

pthread_mutex_getprioceiling(3T)

pthread_mutex_getprioceiling(3T) gets the priority ceiling of a mutex.

#include <pthread.h>

int pthread_mutex_getprioceiling(const pthread_mutexatt_t *mutex, int *prioceiling);

pthread_mutex_getprioceiling() returns the priority ceiling, prioceiling of a mutex mutex.

Return Values

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

If any of the following conditions occurs, pthread_mutexatt_getprioceiling() fails and returns the corresponding value.


ENOSYS

The option _POSIX_THREAD_PRIO_PROTECT is not defined and the implementation does not support the function.

If any of the following conditions occurs, pthread_mutex_getprioceiling() might fail and return the corresponding value.


EINVAL

The value specified by mutex does not refer to a currently existing mutex.


ENOSYS

The implementation does not support the priority ceiling protocol for mutexes.


EPERM

The caller does not have the privilege to perform the operation.

Set Mutex's Robust Attribute

pthread_mutexattr_setrobust_np(3T)

pthread_mutexattr_setrobust_np(3T) sets the robust attribute of a mutex attribute object.

#include <pthread.h>

int pthread_mutexattr_setrobust_np(pthread_mutexatt_t *attr, int *robustness);

Note -

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


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 dies. The value of robustness, defined in pthread.h, is PTHREAD_MUTEX_ROBUST_NP or PTHREAD_MUTEX_STALLED_NP. The default value is PTHREAD_MUTEX_STALLED_NP.

Return Values

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

If any of the following conditions occurs, pthread_mutexattr_setrobust_np() fails and returns the corresponding value.


ENOSYS

The option _POSIX_THREAD_PRIO__INHERIT is not defined or the implementation does not support pthread_mutexattr_setrobust_np().


ENOTSUP

The value specified by robustness is not supported.

pthread_mutexattr_setrobust_np() might fail if:


EINVAL

The value specified by attr or robustness is invalid.

Get Mutex's Robust Attribute

pthread_mutexattr_getrobust_np(3T)

pthread_mutexattr_getrobust_np(3T) gets the robust attribute of a mutex attribute object.

#include <pthread.h>

int pthread_mutexattr_getrobust_np(pthread_mutexatt_t *attr, int *robustness);

Note -

pthread_mutexattr_getrobust_np() applies only if the symbol _POSIX_THREAD_PRIO_INHERIT is defined.


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

robustness is the value of the robust attribute of a mutex attribute object.

Return Values

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

If any of the following conditions occurs, pthread_mutexattr_getrobust_np() fails and returns the corresponding value.


ENOSYS

The option _POSIX_THREAD_PRIO__INHERIT is not defined or the implementation does not support pthread_mutexattr_getrobust_np().


ENOTSUP

The value specified by robustness is not supported.

pthread_mutexattr_getrobust_np() might fail if:


EINVAL

The value specified by attr or robustness is invalid.