C语言 C:如何使用 POSIX 线程声明递归互斥锁?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7037481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: How do you declare a recursive mutex with POSIX threads?
提问by Pithikos
I am a bit confused on how to declare a recursive mutex using pthread. What I try to do is have only one thread at a time be able to run a piece of code(including functions) but after scepticism I figured out that the use of mutexes would not work and that instead I should use recursive mutexes. Here is my code:
我对如何使用 pthread 声明递归互斥体有点困惑。我试图做的是一次只有一个线程能够运行一段代码(包括函数),但在怀疑之后我发现使用互斥锁是行不通的,而我应该使用递归互斥锁。这是我的代码:
pthread_mutex_lock(&mutex); // LOCK
item = queue_peek(queue); // get last item in queue
item_buff=item; // save item to a buffer
queue_removelast(queue); // remove last item from queue
pthread_mutex_unlock(&mutex); // UNLOCK
So what I try to do is just read/remove from the queue serially.
所以我尝试做的只是从队列中串行读取/删除。
The thing is that there isn't any example out there on how to declare recursive mutexes. Or there maybe a few but they don't compile for me.
问题是没有任何关于如何声明递归互斥锁的例子。或者可能有一些,但它们不适合我编译。
回答by Piotr Kukielka
The code from Michael Foukarakis is almost good but he initializes the mutex twice which leads to undefined behavior. It should just be:
Michael Fokarakis 的代码几乎不错,但他将互斥锁初始化了两次,这导致了未定义的行为。它应该只是:
pthread_mutex_t Mutex;
pthread_mutexattr_t Attr;
pthread_mutexattr_init(&Attr);
pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&Mutex, &Attr);
I actually use this code in production, and I know it works correctly on Linux, Solaris, HP-UX, AIX, Mac OSX and FreeBSD.
我实际上在生产中使用了这段代码,我知道它可以在 Linux、Solaris、HP-UX、AIX、Mac OSX 和 FreeBSD 上正常工作。
You also need to add proper linker flag to compile this:
您还需要添加适当的链接器标志来编译它:
AIX, Linux, FreeBSD:
CPLATFORM += -pthread
mingw32:
LDFLAGS += -lpthread
回答by Michael Foukarakis
To create a recursive mutex, use:
要创建递归互斥锁,请使用:
#include <pthread.h>
int pthread_mutexatttr_settype(pthread_mutexattr_t *attr,
int type);
where type is PTHREAD_MUTEX_RECURSIVE.
类型在哪里PTHREAD_MUTEX_RECURSIVE。
Don't forget to check the return value!
不要忘记检查返回值!
Example:
例子:
/* or PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutexattr_t mta;
or alternatively, initialize at runtime (don't do both, it's undefined behaviour):
或者,在运行时初始化(不要两者都做,这是未定义的行为):
pthread_mutexattr_init(&mta);
/* or PTHREAD_MUTEX_RECURSIVE_NP */
pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex, &mta);
回答by Basile Starynkevitch
On Linux (but this is non portable to other systems), if the mutex is a global or static variable, you could initialize it like
在 Linux 上(但这不能移植到其他系统),如果互斥锁是一个全局或静态变量,你可以像这样初始化它
static pthread_mutex_t recmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
(and by the way, the example is from pthread_mutex_init(3)manpages!)
(顺便说一下,这个例子来自pthread_mutex_init(3)手册页!)
回答by hamstergene
You need to add mutex attributes when creating the mutex.
创建互斥锁时需要添加互斥锁属性。
Call pthread_mutexattr_init, then pthread_mutexattr_settypewith PTHREAD_MUTEX_RECURSIVEthen use these attributes with pthread_mutex_init. Read man pthread_mutexattr_initfor more info.
调用pthread_mutexattr_init, then pthread_mutexattr_settypewithPTHREAD_MUTEX_RECURSIVE然后使用这些属性 with pthread_mutex_init。阅读man pthread_mutexattr_init更多信息。

