我有一个多线程程序。由于我想不时重置状态,我想暂停所有线程一段时间。为此,我已经实现了此问题的解决方案之一:What is the best solution to pause and resume pthreads?
pthread_mutex_t suspend_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t resume_cond = PTHREAD_COND_INITIALIZER;
int suspend_flag = 0;
void suspend_threads()
{ // tell the thread to suspend
pthread_mutex_lock(&suspend_mutex);
suspend_flag = 1;
pthread_mutex_unlock(&suspend_mutex);
}
void resume_threads()
{ // tell the thread to resume
pthread_mutex_lock(&suspend_mutex);
suspend_flag = 0;
pthread_cond_broadcast(&resume_cond);
pthread_mutex_unlock(&suspend_mutex);
}
void check_suspend()
{ // if suspended, suspend until resumed
pthread_mutex_lock(&suspend_mutex);
while (suspend_flag != 0) pthread_cond_wait(&resume_cond, &suspend_mutex);
pthread_mutex_unlock(&suspend_mutex);
}
当线程处于方便状态时,线程会非常频繁地调用 check_suspend
。
现在的问题是我收到断言失败:
pthread_mutex_lock.c:62: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed.
[New LWP 1981]
Program received signal SIGABRT, Aborted.
[Switching to LWP 1981]
0x0151f5b0 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:67
67 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
in ../nptl/sysdeps/unix/sysv/linux/raise.c
(gdb) up
#1 0x014e0f90 in abort () at abort.c:121
121 abort.c: No such file or directory.
in abort.c
(gdb) up
#2 0x014db638 in __assert_fail (assertion=0x1a1692c "mutex->__data.__owner == 0", file=0x1a16d90 "pthread_mutex_lock.c", line=62, f
81 assert.c: No such file or directory.
in assert.c
(gdb) up
#3 0x014c82a8 in __pthread_mutex_lock (mutex=0x1fcdc9c) at pthread_mutex_lock.c:62
62 pthread_mutex_lock.c: No such file or directory.
in pthread_mutex_lock.c
(gdb) up
#4 0x014b0694 in check_suspend () at /home/data/programming/broadcom/ssdk/src/customer/epfl_vdp.c:1736
warning: Source file is more recent than executable.
1736 pthread_mutex_lock(&suspend_mutex);
(gdb) p suspend_mutex
$1 = {
__data = {
__lock = 2,
__count = 0,
__owner = 0,
__kind = 0,
__nusers = 0,
{
__spins = 0,
__list = {
__next = 0x0
}
}
},
__size = "\000\000\000\002", '\000' <repeats 19 times>,
__align = 2
}
搜索只提出了一个问题,即一个人试图解锁他没有拥有的锁,但据我所知,代码并非如此。