Multithreaded Programming Guide

Initializing a Mutex Attribute Object

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

pthread_mutexattr_init Syntax

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. See Table 4–2 for information about the attributes in the mattr object.

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

Table 4–2 Default Attribute Values for mattr

Attribute 

Value 

Result 

pshared

PTHREAD_PROCESS_PRIVATE

The initialized mutex can be used within a process. Only those threads created by the same process can operate on the mutex. 

type

PTHREAD_MUTEX_DEFAULT

The Solaris Pthreads implementation maps PTHREAD_MUTEX_DEFAULT to PTHREAD_MUTEX_NORMAL, which does not detect deadlock.

protocol

PTHREAD_PRIO_NONE

Thread priority and scheduling are not affected by the priority of the mutex owned by the thread. 

prioceiling

– 

The prioceiling value is drawn from the existing priority range for the SCHED_FIFO policy, as returned by the sched_get_priority_min() and sched_get_priority_max() functions. This priority range is determined by the Solaris version on which the mutex is created.

robustness

PTHREAD_MUTEX_STALLED_NP

When the owner of a mutex dies, all future calls to pthread_mutex_lock() for this mutex will be blocked from progress.

pthread_mutexattr_init Return Values

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


ENOMEM

Description:

Insufficient memory exists to initialize the mutex attribute object.