https://timsong-cpp.github.io/cppwp/n4861/intro.races#8
An evaluation A is dependency-ordered before an evaluation B if (8.1) A performs a release operation on an atomic object M, and, in another thread, B performs a consume operation on M and reads the value written by A, or (8.2) for some evaluation X, A is dependency-ordered before X and X carries a dependency to B. [ Note: The relation “is dependency-ordered before” is analogous to “synchronizes with”, but uses release/consume in place of release/acquire. — end note ]
“原子对象”在 C++ 中是“特殊的东西”,或者它只是给上下文中的任何变量的名称(当我们在 C++ 中谈论并发时) C++ 中的并发性?
如果它有什么特别之处,你能告诉我它是什么吗?
最佳答案
在相对较新的 C++ 版本之前,原子对象只是 std::atomic
东西。这很简单。现在,它变得更加复杂。
简介atomic_ref
改变了这个:
[atomics.ref.generic] 31.7/1 读取:
An atomic_ref object applies atomic operations ([atomics.general]) to the object referenced by *ptr such that, for the lifetime ([basic.life]) of the atomic_ref object, the object referenced by *ptr is an atomic object ([intro.races]).
所以原子对象是std::atomic<T>
和家人(std::atomic_bool
等),加上任何被 std::atomic_ref<T>
包裹的东西. std::atomic_ref<T>
不是原子的,但是包裹的东西变成了原子。
这个想法是,这些原子对象是原子操作(在 C++ 标准中是一种特殊的线程安全操作)可以应用的对象。
事实上,“普通”对象不再包裹在 atomic_ref
中不再是原子的很重要而且很难考虑。我怀疑这条规则的存在是因为一些平台使用基于锁的操作来实现原子。在其他情况下,原子操作可以发生在大多数对齐良好的内存上。
std::atomic<int> a;
int b;
a
是原子的,b
不是。
{
std::atomic_ref<int> x(b);
现在b
是一个原子对象。
}
现在b
不是原子对象。
关于c++ - Happens-Before 关系 - 在 C++ 中谈论并发的原子对象是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75400642/