带有原子和条件的虚假唤醒
std :: atomic< t>
和std :: procention_variable
俩都有成员等待
和notify_one_one
函数。在某些应用程序中,程序员可能可以选择将任何一种用于同步目的。这些等待
函数的目标之一是它们应与操作系统协调以最大程度地减少虚假唤醒。也就是说,操作系统应避免唤醒等待
-ing线程,直到notify_one
或notify_all
被调用。
在我的计算机上,sizeof(std :: atomic< t>)
is size> sizeof(t)
and sizeof(std :: procenty_variable)
is 72 。 sizeof(std :: atomic< t>)
保留0字节时的目的。
我的问题:我是否应该期望std :: procention_variable
'和std :: atomic< t>
的 等待函数?例如,std :: condition_variable
是否有伪造的唤醒?
std :: atomic< t> :: wait()
std::atomic<T>
and std::condition_variable
both have member wait
and notify_one
functions. In some applications, programmers may have a choice between using either for synchronization purposes. One goal with these wait
functions is that they should coordinate with the operating system to minimize spurious wakeups. That is, the operating system should avoid waking the wait
-ing thread until notify_one
or notify_all
are called.
On my machine, sizeof(std::atomic<T>)
is sizeof(T)
and sizeof(std::condition_variable)
is 72. If you exclude std::atomic<T>
's T
member, then std::condition_variable
reserves 72 bytes for to serve its synchronization purposes while sizeof(std::atomic<T>)
reserves 0 bytes.
My question: should I expect different behavior between std::condition_variable
's and std::atomic<T>
's wait
functions? For example, should std::condition_variable
have fewer spurious wakeups?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
std :: Atomic :: Wait
没有虚假的唤醒。标准保证了观察到变化的值,它在 [atomics.types.generic.general]/30 :因此,如果原子等待的基本实现使虚假唤醒,则C ++标准库实施隐藏了它们。
如果您的问题是关于在中是否有更多或更少的虚假唤醒,那么原子或条件变量的基本实现,则是特定于实施的。将取决于操作系统和库实施。最有可能的答案是:否,因为最终实现OS进行内核呼叫的可能性很可能相同。
std::atomic::wait
does not have spurious wake ups. The standard guarantees that a changed value was observed, it says in [atomics.types.generic.general]/30:So, if the underlying implementation of atomic wait makes spurious wake ups, they are hidden by the C++ standard library implementation.
If your questions is about whether there are more or fewer spurious wakeups in the underlying implementation of atomics or condition variables, then it is implementation specific. Will depend on operating system and library implementation. Most likely answer is: no, because the ultimate implementation, where OS makes kernel call is highly likely the same.