pthread_cond_broadcast与pthread_cond_signal使用区别

引用opengroup的描述

1.下面情况适合用pthread_cond_broadcast

  • 一个生产者多消费者,生产者能一次产生多个产品的情况。
  • 多生产者多消费者
  • 读写锁实现(写入之后,通知所有读者)

2.下面情况适合pthread_cond_signal的情况

  • 单一生产者,生产者一次生产一个产品的情况,最好一个消费者

注意:pthread_cond_signal在单一异步唤醒的处理线程的情况时,是不安全的

APPLICATION USAGE

The pthread_cond_broadcast() function is used whenever the shared-variable state has been changed in a way that more than one thread can proceed with its task. Consider a single producer/multiple consumer problem, where the producer can insert multiple items on a list that is accessed one item at a time by the consumers.(一个生产者多消费者,生产者能一次产生多个产品的情况。) By calling the pthread_cond_broadcast() function, the producer would notify all consumers that might be waiting, and thereby the application would receive more throughput on a multi-processor. In addition, pthread_cond_broadcast() makes it easier to implement a read-write lock.(读写锁) The pthread_cond_broadcast() function is needed in order to wake up all waiting readers when a writer releases its lock. Finally, the two-phase commit algorithm can use this broadcast function to notify all clients of an impending transaction commit.

It is not safe to use the pthread_cond_signal() function in a signal handler that is invoked asynchronously. Even if it were safe, there would still be a race between the test of the Boolean pthread_cond_wait() that could not be efficiently eliminated.

Mutexes and condition variables are thus not suitable for releasing a waiting thread by signaling from code running in a signal handler.

Rreference:
http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cond_broadcast.html

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值