具有内存顺序的原子负载存储 [英] atomic load store with memory order
问题描述
线程A首先运行x.store(1, std::memory_order_release)
,
然后线程B运行x.load(std::memory_order_acquire)
.
不能保证线程B中的x
读取由A存储的1.
Thread A runs x.store(1, std::memory_order_release)
first,
then thread B runs x.load(std::memory_order_acquire)
.
x
in thread B is not guaranteed to read 1 stored by A.
如果我使用memory_order_seq_cst
,是否可以保证读取1?
If I use memory_order_seq_cst
, will it be guaranteed to read 1?
推荐答案
对于一个原子变量的加载/存储,内存顺序之间没有区别.这是因为 std::memory_order
指定在原子操作周围如何对存储器访问(包括常规的非原子存储器访问)进行排序.
There is no difference between memory orderings with regards to load/store of one atomic variable. This is because std::memory_order
specifies how memory accesses, including regular, non-atomic memory accesses, are to be ordered around an atomic operation.
有关详细信息,请阅读 std :: memory_order .特别是:
Read std::memory_order for full details. In particular:
对任何特定原子变量的所有修改都以特定于该原子变量的总顺序发生.
All modifications to any particular atomic variable occur in a total order that is specific to this one atomic variable.
这篇关于具有内存顺序的原子负载存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!