简体   繁体   English

为什么 boost::condition_variable 可以使用 pthread_cond_signal 只唤醒一个线程

[英]Why boost::condition_variable can use pthread_cond_signal to wake up only one thread

In the source code of boost::condition_variable, method condition_variable::notify_one() try to use pthread_cond_signal() to wake up only one thread.在 boost::condition_variable 的源代码中,方法 condition_variable::notify_one() 尝试使用 pthread_cond_signal() 只唤醒一个线程。 https://code.woboq.org/appleseed/include/boost/thread/pthread/condition_variable.hpp.html https://code.woboq.org/appleseed/include/boost/thread/pthread/condition_variable.hpp.html

inline void condition_variable::notify_one() BOOST_NOEXCEPT
    {
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
        boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
#endif
        BOOST_VERIFY(!pthread_cond_signal(&cond));
    }

However, POSIX says:但是,POSIX 说:

The pthread_cond_signal() function shall unblock at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond). pthread_cond_signal() function 应至少解除阻塞在指定条件变量 cond 上阻塞的线程之一(如果任何线程在 cond 上阻塞)。

So why boost::condition_variable make sure that the pthread_cond_signal just wake up one thread???那么为什么 boost::condition_variable 确保 pthread_cond_signal 只唤醒一个线程???

So why boost::condition_variable make sure that the pthread_cond_signal just wake up one thread???那么为什么 boost::condition_variable 确保 pthread_cond_signal 只唤醒一个线程???

Why?为什么? That question is moot.这个问题没有实际意义。 The question is "whether".问题是“是否”。 And it doesn't (as you see).它没有(如你所见)。

You can see that it uses the pthread API there to unblock at least one waiting thread.你可以看到它使用 pthread API 来解除阻塞至少一个等待线程。

This is merely as opposed to notify_all , which uses pthread_cond_broadcast .这与notify_all ,后者使用pthread_cond_broadcast

The distinction is useful because it can improve the efficiency of concurrent operations in situations where waking up all waiters would be wasteful.这种区别很有用,因为它可以在唤醒所有服务员很浪费的情况下提高并发操作的效率。

Related: when using condition variables, you must always take spurious wake-up into account相关:使用条件变量时,必须始终考虑虚假唤醒

You seem to be asking about the name of boost::condition_variable::notify_one() in light of the fact that the pthread-based implementation may in fact wake up more than one thread.您似乎在询问boost::condition_variable::notify_one()名称,因为基于 pthread 的实现实际上可能会唤醒多个线程。

The fact is that although the specifications for pthread_cond_signal() say that it will unblock "at least one" thread blocked on the CV (provided there are any), that is weaselly standard-speak.事实是,尽管pthread_cond_signal()的规范说它将解除阻塞在 CV 上的“至少一个”线程(如果有的话),那是黄鼠狼的标准说法。 Although it is allowed for pthread_cond_signal() to unblock more than one thread, it is usual and expected that if any threads are blocked on the CV when it is signaled, then exactly one will be unblocked.尽管允许pthread_cond_signal()解除对多个线程的阻塞,但通常和预期的是,如果在发出信号时 CV 上有任何线程被阻塞,那么恰好有一个线程将被解除阻塞。 Allowing for more than one to be unblocked allows for implementations to be lighter-weight and more efficient, and it places no new burden on programmers because the same measures that handle spurious wakeups should be equally effective against extra wakeups, supposing that you even choose to distinguish between those.允许不止一个被解锁允许实现更轻量级和更高效,并且它不会给程序员带来新的负担,因为处理虚假唤醒的相同措施应该同样有效地防止额外唤醒,假设您甚至选择区分那些。

Thus, boost::condition_variable::notify_one() is so named because "notify_one" accurately represents a behavior that programmers are anticipated sometimes to want, and the so-named method is the way for them to request it.因此, boost::condition_variable::notify_one()之所以如此命名,是因为“notify_one”准确地代表了程序员有时会想要的行为,而所谓的方法是他们请求它的方式。 That occasionally it delivers something a bit different is a characteristic of a very wide variety of functions.偶尔它会提供一些不同的东西,这是非常广泛的功能的一个特征。 It is not helpful to try to capture all the details in the name.尝试捕获名称中的所有细节是没有帮助的。 Not only would you end up with unreadably long names, such as notify_at_least_one_if_any_are_waiting , but you would lose the sense of the primary expectation for the function's behavior.您不仅会得到难以理解的长名称,例如notify_at_least_one_if_any_are_waiting ,而且您会失去对函数行为的主要期望的意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM