带有原子和条件的虚假唤醒

发布于 2025-01-28 08:24:22 字数 1367 浏览 3 评论 0原文

std :: atomic< t>std :: procention_variable俩都有成员等待notify_one_one函数。在某些应用程序中,程序员可能可以选择将任何一种用于同步目的。这些等待函数的目标之一是它们应与操作系统协调以最大程度地减少虚假唤醒。也就是说,操作系统应避免唤醒等待 -ing线程,直到notify_onenotify_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<

std :: procention_variable :: )

std :: prenti其> ()

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?

std::atomic<T>::wait()

std::atomic<T>::notify_one()

std::condition_variable::wait()

std::condition_variable::notify_one()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽心 2025-02-04 08:24:22

我的问题:我应该期望std :: condition_variable's和std :: atomic&lt; t&gt;'s 等待功能?例如,std :: condition_variable较少的虚假唤醒?

std :: Atomic :: Wait没有虚假的唤醒。标准保证了观察到变化的值,它在 [atomics.types.generic.general]/30

效果:反复执行以下步骤:

(30.1)
评估负载(顺序),并比较其平等的价值表示与旧的。

(30.2)
如果它们比较不平等,则返回。

(30.3)
块,直到被原子通知操作或没有阻碍的原子封锁为止。

因此,如果原子等待的基本实现使虚假唤醒,则C ++标准库实施隐藏了它们。

如果您的问题是关于在中是否有更多或更少的虚假唤醒,那么原子或条件变量的基本实现,则是特定于实施的。将取决于操作系统和库实施。最有可能的答案是:否,因为最终实现OS进行内核呼叫的可能性很可能相同。

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?

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:

Effects: Repeatedly performs the following steps, in order:

(30.1)
Evaluates load(order) and compares its value representation for equality against that of old.

(30.2)
If they compare unequal, returns.

(30.3)
Blocks until it is unblocked by an atomic notifying operation or is unblocked spuriously.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文