来自手册页:
The
pthread_mutex_init()
function shall initialize the mutex referenced by mutex with attributes specified by attr.
If attr is NULL, the default mutex attributes are used; the effect shall be the same as passing the address of a default mutex attributes object. Upon successful initialization, the state of the mutex becomes initialized and unlocked.
默认的互斥量属性是什么?
此代码设置了什么:pthread_mutex_t mutexLock = PTHREAD_MUTEX_INITIALIZER;
?
最佳答案
您必须仔细研究 POSIX 规范才能获得互斥属性的默认值:
-
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.
请注意,这可能与其他互斥锁类型不同,但它可以映射到
PTHREAD_MUTEX_NORMAL
或PTHREAD_MUTEX_ERRORCHECK
。 pthread_mutexattr_setpshared()
The default value of the attribute is
PTHREAD_PROCESS_PRIVATE
.
对于其他属性,规范似乎没有明确说明。这些属性是:
- 优先级上限属性 (
pthread_mutexattr_getprioceiling()
) - 优先继承的协议(protocol)属性(
pthread_mutexattr_getprotocol()
)
关于c - 什么是默认互斥锁属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14642034/