【发布时间】:2015-06-17 11:39:33
【问题描述】:
Summary
-----------
1. In main() am going for pthread_cond_wait().
2. In signal handler() am waking main() using pthread_cond_signal().
3. But main() is not coming out from pthread_cond_wait().
这里有什么问题?帮帮我。
#include <stdio.h>
myclass *myObj = NULL;
我主要是在等待一个信号:
int main()
{
myObj = new myclass;
/* do something */
myobj->gotoWait(); <=== Wait blocked for ever.
/* do clean up here */
return 0;
}
向主线程发送信号的信号处理程序:
static void signalHandler(int sig, siginfo_t *siginfo, void *context)
{
myObj->wakeFromWait();
}
实现信号等待和发送的实际类。
这里有什么问题?
myclass::gotoWait()
{
pthread_mutex_lock(&mtx);
pthread_cond_wait(&cnd, &mtx);
pthread_mutex_unlock(&mtx);
}
myclass::wakeFromWait()
{
pthread_mutex_lock(&mtx);
pthread_cond_signal(&cnd, &mtx);
pthread_mutex_unlock(&mtx);
}
【问题讨论】:
-
这里有什么问题? ①你没有给我们MCVE。 ② 您正在使用没有谓词的条件变量,即使没有虚假唤醒,这对于任何重要的应用程序都可能是不正确的。 ③ 你等待一个条件变量而不期待spurious wakeups。 ④ 你错误地调用了
pthread_cond_wait——这是怎么编译的? ⑤ 你在信号处理器中做了不安全的事情。