免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5731 | 回复: 8
打印 上一主题 下一主题

刚刚发现pthread_cond_signal会唤醒不止一个线程 [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-11 14:08 |只看该作者 |倒序浏览
man pthread_cond_signal


  1. The  pthread_cond_signal()  function  shall  unblock  at  least one of the threads that are blocked on the specified condition variable cond (if any
  2.        threads are blocked on cond).

复制代码

论坛徽章:
0
2 [报告]
发表于 2008-04-11 14:22 |只看该作者
如果想唤醒所有线程,请用pthread_cond_broadcast
唤醒单个线程有时是不好用的。

论坛徽章:
0
3 [报告]
发表于 2008-04-11 14:34 |只看该作者
结合mutex来用就没问题啊,
只有一个线程能先得到mutex, (然后可以设个flag)
后得到mutex的线程,看flag条件不满足再去conditon wait就是了

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2008-04-11 14:40 |只看该作者

回复 #3 芙蓉 的帖子

呵呵..出错是不会出错...只是以前总以为只会唤醒一个线程呢..可能这就是条件变量为什么带一个mutex的原因之一吧

论坛徽章:
0
5 [报告]
发表于 2008-04-11 16:23 |只看该作者
这是没办法的事情,只能结合锁来用
或者用管道机制来实现阻塞/唤醒

论坛徽章:
0
6 [报告]
发表于 2008-04-11 21:15 |只看该作者
原帖由 cookis 于 2008-4-11 14:40 发表
呵呵..出错是不会出错...只是以前总以为只会唤醒一个线程呢..可能这就是条件变量为什么带一个mutex的原因之一吧


带个mutex是为了检查条件时不出现误检吧

论坛徽章:
0
7 [报告]
发表于 2010-07-17 00:08 |只看该作者
条件变量在实现的时候可能无法保证仅仅唤醒一个线程,因此醒来后再看下条件是对的

论坛徽章:
0
8 [报告]
发表于 2010-07-17 12:27 |只看该作者
为啥我 ubuntu的 man手册里是这么写的呢?

thread_cond_signal restarts one of the threads that are waiting on the
condition  variable  cond.   If no threads are waiting on cond, nothing
happens. If several  threads  are  waiting  on  cond,  exactly  one  is
restarted, but it is not specified which.

论坛徽章:
0
9 [报告]
发表于 2010-07-17 12:30 |只看该作者
不过 3P 里是lz那么写的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP