PTHREAD_MUTEXATTR(3) | Library Functions Manual | PTHREAD_MUTEXATTR(3) |
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
#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);
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
PTHREAD_MUTEX_NORMAL
mutex will result in
undefined behavior.PTHREAD_MUTEX_ERRORCHECK
PTHREAD_MUTEX_ERRORCHECK
mutex without first
dropping the lock, an error will be returned. If a thread attempts to
unlock a PTHREAD_MUTEX_ERRORCHECK
mutex that is
locked by another thread, an error will be returned. If a thread attempts
to unlock a PTHREAD_MUTEX_ERRORCHECK
thread that
is unlocked, an error will be returned.PTHREAD_MUTEX_RECURSIVE
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 a
PTHREAD_MUTEX_RECURSIVE
mutex that is locked by
another thread, an error will be returned. If a thread attempts to unlock
a PTHREAD_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
PTHREAD_MUTEX_DEFAULT
mutex locked by
another thread will result in undefined behavior. Attempts to unlock an
already unlocked PTHREAD_MUTEX_DEFAULT
mutex will
result in undefined behavior. This is the default mutex type for
pthread_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
PTHREAD_MUTEX_POLICY_FAIRSHARE_NP
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.
If successful, these functions return 0. Otherwise, an error number is returned to indicate the error.
The following environment variables change the behavior of the pthread mutex implementation.
PTHREAD_MUTEX_DEFAULT_POLICY
pthread_mutexattr_setpolicy_np
(). The valid values
are mapped as:
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.
The pthread_mutexattr_init
() function
shall fail if:
ENOMEM
]The pthread_mutexattr_destroy
() function
will fail if:
EINVAL
]The pthread_mutexattr_setprioceiling
()
function will fail if:
EINVAL
]The pthread_mutexattr_getprioceiling
()
function will fail if:
EINVAL
]The pthread_mutexattr_setprotocol
()
function will fail if:
EINVAL
]The pthread_mutexattr_getprotocol
()
function will fail if:
EINVAL
]The pthread_mutexattr_settype
() function
shall fail if:
EINVAL
]The pthread_mutexattr_gettype
() function
will fail if:
EINVAL
]The pthread_mutexattr_setpolicy_np
()
function will fail if:
EINVAL
]The pthread_mutexattr_getpolicy_np
()
function will fail if:
EINVAL
]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”)
July 9, 2010 | macOS 15.2 |