pthread_cond_signal()和pthread_cond_wait()

Daneill 2010-12-21 01:49:55
会不会有这样的情况:pthread_cond_signal(cv)先于pthread_cond_wait(cv,mutex)执行,而导致唤醒信号丢失,使得程序阻塞在pthread_cond_wait(cv,mutex)处?

请高手指点,非常感谢!!!O(∩_∩)O~
...全文
272 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
XStack 2012-06-14
  • 打赏
  • 举报
回复
楼主,你这个问题最后是彼解决的,我也遇到这个问题了,好头痛啊。
Daneill 2011-01-06
  • 打赏
  • 举报
回复
好,谢谢各位高手!非常感谢!
freshui 2010-12-27
  • 打赏
  • 举报
回复
当然会了。
之前我做ecos的时候,碰到这个bug,解了几天才搞定。后来分析ecos的源代码,发现他做这个之前,先lock_dsr,基本上等于关中断,禁掉调用。才能工作。
流程是:

关中断
调用可能会产生signal的函数
wait signal
开中断

yum_yum 2010-12-27
  • 打赏
  • 举报
回复
去linux社区看看
justkk 2010-12-27
  • 打赏
  • 举报
回复
该贴到linux专区
是会丢失的

The pthread_cond_broadcast() and pthread_cond_signal() functions shall have no effect if there are no threads currently blocked on cond.
If no thread is blocked on the condition, the subroutine succeeds, but the signalling of the condition is not held. The next thread calling pthread_cond_wait will be blocked.
Daneill 2010-12-27
  • 打赏
  • 举报
回复
为什么没人回答哩!!!呼唤高手啊
Daneill 2010-12-21
  • 打赏
  • 举报
回复
写了一个代码测试了一下
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
pthread_mutex_t cvMutex = PTHREAD_MUTEX_INITIALIZER;

void * run1(void *){
Sleep(1000);
pthread_mutex_lock(&cvMutex);
pthread_cond_wait(&cv,&cvMutex);
pthread_mutex_unlock(&cvMutex);
cout<<"finish run1"<<endl;
return NULL;
}

void * run2(void *){
pthread_mutex_lock(&cvMutex);
pthread_cond_signal(&cv);
pthread_mutex_unlock(&cvMutex);
cout<<"finish run2"<<endl;
return NULL;
}

int main(){
pthread_t t1, t2;
pthread_create(&t1, NULL, run1, NULL);
pthread_create(&t2, NULL, run2, NULL);

return 0;
}

最后的运行结果是输入finish run2,然后卡在wait处了。
请教高手:平时遇到条件变量,如何防止信号丢失的?万分感激!!!!!!

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧