Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

pthread_mutexattr_create(3)

pthread_mutexattr_getkind_np(3)

pthread_mutex_init(3)

pthread_mutexattr_setkind_np(3)  —  Subroutines

Digital

NAME

pthread_mutexattr_setkind_np − Specifies the mutex type attribute that is used when a mutex is created. 

SYNOPSIS

#include <pthread.h>
int pthread_mutexattr_setkind_np(

pthread_mutexattr_t ∗attr ,
int kind );

PARAMETERS

attrMutex attributes object modified. 

kindNew value for the mutex type attribute. The kind parameter specifies the type of mutex that is created. Valid values are MUTEX_FAST_NP (default) and MUTEX_RECURSIVE_NP. 

DESCRIPTION

This routine sets the mutex type attribute that is used when a mutex is created. 

A fast mutex locks and unlocks a mutex in the fastest manner possible. A fast mutex can only be locked (obtained) once. All subsequent calls to pthread_mutex_lock cause the calling thread to block until the mutex is freed by the thread that owns it. If the thread that owns the mutex attempts to lock it again, the thread waits for itself to release the mutex (causing a deadlock). 

A recursive mutex can be locked more than once by the same thread without causing that thread to deadlock. In other words, a single thread can make consecutive calls to pthread_mutex_lock without blocking.  The thread must then call pthread_mutex_unlock the same number of times as it called pthread_mutex_lock before another thread can lock the mutex. 

A recursive mutex is useful when a routine needs to lock a mutex to protect data for exclusive access by the current thread, and it calls another routine (or itself) that also needs to protect data for exclusive access.  Using a recursive mutex allows nested attempts to lock the mutex to succeed rather than deadlock. The application must be coded so that such routines operating on the same set of shared data work together cooperatively. 

A recursive mutex should never be used with condition variables, because the implicit unlock performed for a pthread_cond_wait or pthread_cond_timedwait might not actually release the mutex. In that case, no other thread can satisfy the condition of the predicate. 

RETURN VALUES

If an error condition occurs, this routine returns −1 and sets errno to the corresponding error value. Possible return values are as follows:

Return Error Description
 0 Successful completion.
−1 [EINVAL] The value specified by attr or kind is invalid.
−1 [ESRCH] The value specified by attr does not refer to an existing mutex attributes object. 

RELATED INFORMATION

pthread_mutexattr_create(3), pthread_mutexattr_getkind_np(3), pthread_mutex_init(3)

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026