是否应该立即加载获取立即查看商店发布? [英] Should load-acquire see store-release immediately?
问题描述
假设我们有一个简单变量( std :: atomic< int> var
)和2个线程 T1
T2
,我们有 T1
的以下代码:
Suppose we have one simple variable(std::atomic<int> var
) and 2 threads T1
and T2
and we have the following code for T1
:
...
var.store(2, mem_order);
...
和 T2
...
var.load(mem_order)
...
此外,假设 T2
> T1
(store)的时间(稍后在C ++标准的修改顺序中)。
我对这种情况的理解如下(针对不同的内存顺序):
Also let's assume that T2
(load) executes 123ns later in time(later in the modification order in terms of the C++ standard) than T1
(store).
My understanding of this situation is as follows(for different memory orders):
-
memory_order_seq_cst
-
T2
加载必须加载2
。因此,必须加载最新的值(正如RMW操作的情况一样)。 -
memory_order_acquire
/memory_order_release
/memory_order_relaxed
-T2
没有义务加载2
,但可以加载任何旧的值与唯一的限制:该值不应该比该线程加载的最新的。因此,例如var.load
返回0
。
memory_order_seq_cst
-T2
load is obliged to load2
. So effectively it has to load the latest value(just as it is the case with the RMW operations)memory_order_acquire
/memory_order_release
/memory_order_relaxed
-T2
is not obliged to load2
but can load any older value with the only restriction: that value should not be older than the latest loaded by that thread. So, for examplevar.load
returns0
.
我是否理解了?
UPDATE1:
如果我错误的推理,请提供C ++标准的文本,证明它。
If I'm wrong with the reasoning, please provide the text from the C++ standard which proofs it. Not just theoretical reasoning of how some architecture might work.
推荐答案
没有找到任何参数证明我的理解错误,我认为它是正确的,我的证明如下:
Having found no arguments to prove my understanding wrong I deem it correct and my proof is as follows:
memory_order_seq_cst - T2加载必须加载2。
memory_order_seq_cst - T2 load is obliged to load 2.
这是正确的,因为使用 memory_order_seq_cst
的所有操作都应该对所有内存操作的atomic变量形成单个总顺序。
标准摘录:
That's correct because all operations using memory_order_seq_cst
should form the single total order on the atomic variable of all the memory operations.
Excerpt from the standard:
[29.9 / 3] S在所有memory_order_seq_cst
操作上与所有受影响的位置的发生在之前顺序和
修改顺序一致,这样每个
memory_order_seq_cst操作B从原子
对象M观察以下值之一<...>
[29.9/3] There shall be a single total order S on all memory_order_seq_cst operations, consistent with the "happens before" order and modification orders for all affected locations, such that each memory_order_seq_cst operation B that loads a value from an atomic object M observes one of the following values <...>
我的问题的下一点:
memory_order_acquire / memory_order_release / memory_order_relaxed - T2是
没有义务加载2,但可以加载任何旧的值<...>
memory_order_acquire/memory_order_release/memory_order_relaxed - T2 is not obliged to load 2 but can load any older value <...>
我没有找到任何证据,这可能表明修改后的订单中执行的加载应该看到最新的值。我发现存储/加载操作与任何内存顺序不同于 memory_order_seq_cst
的唯一点是这些:
I didn't find any evidences which might indicate that the load executed later in the modification order should see the latest value. The only points I found for the store/load operations with any memory order different from the memory_order_seq_cst
are these:
[29.3 / 12] 实现应该在合理的时间内使原子库对原子
可见。
[29.3/12] Implementations should make atomic stores visible to atomic loads within a reasonable amount of time.
和
[1.10 / 28] 确保由原子或同步操作
分配的最后一个值(在修改顺序中)在有限的时间内对所有其他线程可见。
[1.10/28] An implementation should ensure that the last value (in modification order) assigned by an atomic or synchronization operation will become visible to all other threads in a finite period of time.
因此,我们唯一的保证是,写入的变量在一段时间内是可见的 - 这是相当合理的保证,但它并不意味着立即可见的先前的存储。这证明了我的第二点。
So the only guarantee we have is that the variable written will be visible within some time - that's pretty reasonable guarantee but it doesn't imply immediate visibility of the previous store. And it proofs my second point.
鉴于我最初的理解是正确的。
Given all that my initial understanding was correct.
这篇关于是否应该立即加载获取立即查看商店发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!