NAME
pthread_mutexattr_init
,
pthread_mutexattr_destroy
,
pthread_mutexattr_setprioceiling
,
pthread_mutexattr_getprioceiling
,
pthread_mutexattr_setprotocol
,
pthread_mutexattr_getprotocol
,
pthread_mutexattr_settype
,
pthread_mutexattr_gettype
—
mutex attribute operations
SYNOPSIS
#include
<pthread.h>
int
pthread_mutexattr_init
(pthread_mutexattr_t
*attr);
int
pthread_mutexattr_destroy
(pthread_mutexattr_t
*attr);
int
pthread_mutexattr_setprioceiling
(pthread_mutexattr_t
*attr, int
prioceiling);
int
pthread_mutexattr_getprioceiling
(pthread_mutexattr_t
*attr, int
*prioceiling);
int
pthread_mutexattr_setprotocol
(pthread_mutexattr_t
*attr, int
protocol);
int
pthread_mutexattr_getprotocol
(pthread_mutexattr_t
*attr, int
*protocol);
int
pthread_mutexattr_settype
(pthread_mutexattr_t
*attr, int
type);
int
pthread_mutexattr_gettype
(pthread_mutexattr_t
*attr, int
*type);
int
pthread_mutexattr_setpolicy_np
(pthread_mutexattr_t
*attr, int
policy);
int
pthread_mutexattr_getpolicy_np
(pthread_mutexattr_t
*attr, int
*policy);
DESCRIPTION
Mutex attributes are used to specify parameters to
pthread_mutex_init
().
Like with thread attributes, one attribute object can be used in multiple
calls to
pthread_mutex_init(3), with or without modifications between
calls.
The
pthread_mutexattr_init
()
function initializes attr with all the default mutex
attributes.
The
pthread_mutexattr_destroy
()
function destroys attr.
The
pthread_mutexattr_settype
()
functions set the mutex type value of the attribute.
Valid mutex types are:
PTHREAD_MUTEX_NORMAL
- This type of mutex does not check for usage errors. It will deadlock if
reentered, and result in undefined behavior if a locked mutex is unlocked
by another thread. Attempts to unlock an already unlocked
PTHREAD_MUTEX_NORMAL
mutex will result in undefined behavior. PTHREAD_MUTEX_ERRORCHECK
- These mutexes do check for usage errors. If an attempt is made to relock a
PTHREAD_MUTEX_ERRORCHECK
mutex without first dropping the lock, an error will be returned. If a thread attempts to unlock aPTHREAD_MUTEX_ERRORCHECK
mutex that is locked by another thread, an error will be returned. If a thread attempts to unlock aPTHREAD_MUTEX_ERRORCHECK
thread that is unlocked, an error will be returned. PTHREAD_MUTEX_RECURSIVE
- These mutexes allow recursive locking. An attempt to relock a
PTHREAD_MUTEX_RECURSIVE
mutex that is already locked by the same thread succeeds. An equivalent number of pthread_mutex_unlock(3) calls are needed before the mutex will wake another thread waiting on this lock. If a thread attempts to unlock aPTHREAD_MUTEX_RECURSIVE
mutex that is locked by another thread, an error will be returned. If a thread attempts to unlock aPTHREAD_MUTEX_RECURSIVE
thread that is unlocked, an error will be returned.It is advised that
PTHREAD_MUTEX_RECURSIVE
mutexes are not used with condition variables. This is because of the implicit unlocking done by pthread_cond_wait(3) and pthread_cond_timedwait(3). PTHREAD_MUTEX_DEFAULT
- Also this type of mutex will cause undefined behavior if reentered.
Unlocking a
PTHREAD_MUTEX_DEFAULT
mutex locked by another thread will result in undefined behavior. Attempts to unlock an already unlockedPTHREAD_MUTEX_DEFAULT
mutex will result in undefined behavior. This is the default mutex type forpthread_mutexaddr_init
().
pthread_mutexattr_gettype
()
functions copy the type value of the attribute to the location pointed to by
the second parameter.
The
pthread_mutexattr_setpolicy_np
()
function sets the mutex policy value of the attribute.
Valid mutex policies are:
PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
- The first-fit mutex policy allows acquisition of the mutex to occur in any order. This policy is similar in operation to os_unfair_lock, new contending acquirers may obtain ownership of the mutex ahead of existing waiters.
PTHREAD_MUTEX_POLICY_FAIRSHARE_NP
- The fairshare mutex policy guarantees that ownership of a contended mutex will be granted to waiters on a strictly ordered first-in, first-out basis. That is, a mutex holder that unlocks the mutex and then attempts to relock will wait behind existing threads already waiting on the mutex before being granted ownership again.
The
pthread_mutexattr_getpolicy_np
()
function copies the mutex policy value of the
attribute to the location pointed to by the second parameter.
The
pthread_mutexattr_set*
()
functions set the attribute that corresponds to each function name.
The
pthread_mutexattr_get*
()
functions copy the value of the attribute that corresponds to each function
name to the location pointed to by the second function parameter.
RETURN VALUES
If successful, these functions return 0. Otherwise, an error number is returned to indicate the error.
ENVIRONMENT
The following environment variables change the behavior of the pthread mutex implementation.
PTHREAD_MUTEX_DEFAULT_POLICY
- Controls the process-wide policy used when initializing a pthread_mutex_t
that has not had a policy set via
pthread_mutexattr_setpolicy_np
(). The valid values are mapped as:
BACKWARDS COMPATIBILITY
Prior to macOS 10.14 (iOS and tvOS 12.0, watchOS 5.0) the only
available pthread mutex policy mode was
PTHREAD_MUTEX_POLICY_FAIRSHARE_NP
. macOS 10.14 (iOS
and tvOS 12.0, watchOS 5.0) introduces
PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
and also makes this
the default mode for mutexes initialized without a policy attribute set.
Attempting to use
pthread_mutexattr_setpolicy_np
()
to set the policy of a pthread_mutex_t to
PTHREAD_MUTEX_POLICY_FIRSTFIT_NP
on earlier releases
will fail with EINVAL
and the mutex will continue to
operate in fairshare mode.
ERRORS
The pthread_mutexattr_init
() function
shall fail if:
- [
ENOMEM
] - Out of memory.
The pthread_mutexattr_destroy
() function
will fail if:
- [
EINVAL
] - Invalid value for attr.
The pthread_mutexattr_setprioceiling
()
function will fail if:
- [
EINVAL
] - Invalid value for attr, or invalid value for prioceiling.
The pthread_mutexattr_getprioceiling
()
function will fail if:
- [
EINVAL
] - Invalid value for attr.
The pthread_mutexattr_setprotocol
()
function will fail if:
- [
EINVAL
] - Invalid value for attr, or invalid value for protocol.
The pthread_mutexattr_getprotocol
()
function will fail if:
- [
EINVAL
] - Invalid value for attr.
The pthread_mutexattr_settype
() function
shall fail if:
- [
EINVAL
] - The value specified either by type or attr is invalid.
The pthread_mutexattr_gettype
() function
will fail if:
- [
EINVAL
] - Invalid value for attr.
The pthread_mutexattr_setpolicy_np
()
function will fail if:
- [
EINVAL
] - Invalid value for attr.
The pthread_mutexattr_getpolicy_np
()
function will fail if:
- [
EINVAL
] - The value specified either by type or attr is invalid.
SEE ALSO
STANDARDS
The pthread_mutexattr_init
() and
pthread_mutexattr_destroy
() functions conform to
ISO/IEC 9945-1:1996 (“POSIX.1”)
The pthread_mutexattr_setprioceiling
(),
pthread_mutexattr_getprioceiling
(),
pthread_mutexattr_setprotocol
(),
pthread_mutexattr_getprotocol
(),
pthread_mutexattr_settype
(), and
pthread_mutexattr_gettype
() functions conform to
Version 2 of the Single UNIX Specification
(“SUSv2”)