引用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