std :: atomic和std :: condition_variable等待,notify_ *方法之间的区别 [英] Difference between std::atomic and std::condition_variable wait, notify_* methods

查看:121
本文介绍了std :: atomic和std :: condition_variable等待,notify_ *方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览原子操作库",并遇到了原子"wait"和"notify_ "方法的c ++ 20新功能.我很好奇std :: condition_variable的"wait"和"notify _ "方法之间的区别.

I was looking through 'Atomic operations library' and came across a new c++20 feature of atomic 'wait' and 'notify_' methods. I am curious on what the differences are in regards to std::condition_variable's 'wait' and 'notify_' methods.

推荐答案

std:atomic wait notify_all notify_one 方法类似于条件变量的方法.它们允许使用更高效,更轻便的原子变量来实现以前需要条件变量的逻辑.

std:atomic wait, notify_all and notify_one methods are similar to methods of conditional variables. They allow the implementation of the logic that previously required conditional variable by using much more efficient and lightweight atomic variables.

wait 函数将阻塞线程,直到原子对象的值被修改为止.它需要一个参数来与原子对象的值进行比较.并反复执行:

The wait function blocks the thread until the value of the atomic object modifies. It takes an argument to compare with the value of the atomic object. And it repeatedly performs:

  • 如果值相等,它将阻塞线程,直到得到 notify_one notify_all 的通知,或者线程被虚假地解除阻塞.
  • 否则,返回.
  • If the values are equal, it blocks the thread until notified by notify_one or notify_all, or the thread is unblocked spuriously.
  • Otherwise, returns.

注意:即使值已更改,也可以保证 wait 返回,即使底层实现被虚假地阻止.

NOTE: wait is guaranteed to return only if the value has changed, even if underlying implementation unblocks spuriously.

您可以在此处找到实现: https://github.com/ogiroux/atomic_wait/

You can find the implementation here: https://github.com/ogiroux/atomic_wait/.

通过平台以这种方式选择策略:

The strategy is chosen this way, by platform:

  • Linux:默认为futex(带表),回退为futex(无表)->简历->定时退避->旋转.
  • Mac:默认为CV(表格),后退为定时补偿->旋转.
  • Windows:默认为futex(无表),后退为定时退避->旋转.
  • CUDA:默认为定时退避,后退为旋转.(这不是在此树中全部选中的.)
  • 身份不明的平台:默认为旋转.

这篇关于std :: atomic和std :: condition_variable等待,notify_ *方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆