I'd like to add some debugging code to an abstraction of pthread_cond_wait
in my code to check that the calling code really holds the mutex, as it should. This is to check correctness of the rest of the callers.
我想在我的代码中为pthread_cond_wait的抽象添加一些调试代码,以检查调用代码是否真正保存互斥对象,因为它应该保存互斥对象。这是为了检查其他调用者的正确性。
Is there a way to check if the mutex is locked, or enable a debug mode in the pthreads implementation (on Linux) to tell me if it's not?
有没有办法检查互斥体是否被锁定,或者在pthreads实现(在Linux上)中启用调试模式,告诉我它是否被锁定?
3 个解决方案
#1
3
If you create the mutex as an error-checking mutex, using:
如果将互斥对象创建为错误检查互斥对象,使用:
pthread_mutexattr_t attr;
pthread_mutex_t errchkmutex;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutex_init(&errchkmutex, &attr);
...then pthread_cond_wait()
will return EPERM
if the mutex is not locked by the calling thread.
…然后,如果未被调用线程锁定互斥对象,pthread_cond_wait()将返回EPERM。
(of course you would add error-checking to that mutex initialisation code).
(当然,您将向互斥体初始化代码添加错误检查)。
I think error-checking mutexes are exactly the kind of "debugging mode" that you're looking for.
我认为错误检查互斥体正是您要寻找的那种“调试模式”。
#2
0
pthread_cond_wait
will fail unless the mutex is locked by the calling thread. Are you checking return values of pthread functions? If not, this is a recipe for disaster, moreso with threading than with other system calls.
除非互斥体被调用线程锁定,否则pthread_cond_wait将失败。您是否正在检查pthread函数的返回值?如果没有,这是一个灾难的处方,moreso与线程比其他系统调用。
#3
0
The answer, pieced together from zvrba and "R" and some other research, is to create the mutexes with error-checking attributes and check the return value of pthread_cond_wait()
.
从zvrba和“R”以及其他一些研究中拼凑出来的答案是,创建具有错误检查属性的互斥体,并检查pthread_cond_wait()的返回值。
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_ERRORCHECK);
Other relevant info:
其他相关信息:
-
My glibc requires
#define _GNU_SOURCE
before any includes to enablepthread_mutexattr_settype()
我的glibc要求在包含之前使用#define _GNU_SOURCE来启用pthread_mutexattr_settype()
-
I found I do have the POSIX manpages installed as well as the "LinuxThreads" ones. I'm not sure what (ubuntu) packages the various ones came from, but editing
/etc/manpath.config
to put "3posix
" first means I get the right ones now.我发现我确实安装了POSIX manpages以及“LinuxThreads”。我不确定(ubuntu)包来自什么,但是编辑/etc/ manpathn。把“3posix”放在前面意味着我现在得到了正确的。
#1
3
If you create the mutex as an error-checking mutex, using:
如果将互斥对象创建为错误检查互斥对象,使用:
pthread_mutexattr_t attr;
pthread_mutex_t errchkmutex;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutex_init(&errchkmutex, &attr);
...then pthread_cond_wait()
will return EPERM
if the mutex is not locked by the calling thread.
…然后,如果未被调用线程锁定互斥对象,pthread_cond_wait()将返回EPERM。
(of course you would add error-checking to that mutex initialisation code).
(当然,您将向互斥体初始化代码添加错误检查)。
I think error-checking mutexes are exactly the kind of "debugging mode" that you're looking for.
我认为错误检查互斥体正是您要寻找的那种“调试模式”。
#2
0
pthread_cond_wait
will fail unless the mutex is locked by the calling thread. Are you checking return values of pthread functions? If not, this is a recipe for disaster, moreso with threading than with other system calls.
除非互斥体被调用线程锁定,否则pthread_cond_wait将失败。您是否正在检查pthread函数的返回值?如果没有,这是一个灾难的处方,moreso与线程比其他系统调用。
#3
0
The answer, pieced together from zvrba and "R" and some other research, is to create the mutexes with error-checking attributes and check the return value of pthread_cond_wait()
.
从zvrba和“R”以及其他一些研究中拼凑出来的答案是,创建具有错误检查属性的互斥体,并检查pthread_cond_wait()的返回值。
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_ERRORCHECK);
Other relevant info:
其他相关信息:
-
My glibc requires
#define _GNU_SOURCE
before any includes to enablepthread_mutexattr_settype()
我的glibc要求在包含之前使用#define _GNU_SOURCE来启用pthread_mutexattr_settype()
-
I found I do have the POSIX manpages installed as well as the "LinuxThreads" ones. I'm not sure what (ubuntu) packages the various ones came from, but editing
/etc/manpath.config
to put "3posix
" first means I get the right ones now.我发现我确实安装了POSIX manpages以及“LinuxThreads”。我不确定(ubuntu)包来自什么,但是编辑/etc/ manpathn。把“3posix”放在前面意味着我现在得到了正确的。