c - 什么是默认互斥锁属性?

标签 c pthreads mutex

来自手册页:

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_mutexattr_settype()

    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_NORMALPTHREAD_MUTEX_ERRORCHECK

  • pthread_mutexattr_setpshared()

    The default value of the attribute is PTHREAD_PROCESS_PRIVATE.

对于其他属性,规范似乎没有明确说明。这些属性是:

关于c - 什么是默认互斥锁属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14642034/

相关文章:

mutex - 在使用 POSIX 消息队列时,是否应该使用互斥锁来确保并发性?

c - 套接字发送客户端-服务器文件

linux - Linux 内核 v2.6+ 中的 pthread 与 kthread

windows - Win32 Event 对象是递归互斥体吗?

c - 给定递归程序的空间复杂度

Android NDK pthread_create 显示 “too many arguments, expected 1”

go - sync.mutex 锁定什么?

c++ - 从 char** 到 const char** 的隐式转换

c - 两个 MPI_Allreduce() 函数不起作用,给出 NULL Communicator 错误

c - 在 C 中使用带有结构的指定初始值设定项