[英]Memory model release/acquire mode interactions of relaxed atomic operations
The GCC Wiki says this about the memory model synchronization mode Acquire/Release : GCC Wiki 关于内存模型同步模式Acquire/Release 是这样说的:
To make matters a bit more complex, the interactions of non-atomic variables are still the same.
更复杂的是,非原子变量的相互作用仍然相同。 Any store before an atomic operation must be seen in other threads that synchronize.
原子操作之前的任何存储都必须在其他同步线程中看到。 For example:
例如:
-Thread 1-Since 'y' is not an atomic variable, the store to 'y' happens-before the store to 'x', so the assert cannot fail in this case.-线程 1-\n y = 20;
y = 20;\n x.store (10, memory_order_release);
x.store (10, memory_order_release);\n\n -Thread 2-
-线程 2-\n if (x.load(memory_order_acquire) == 10)
如果(x.load(memory_order_acquire)== 10)\n assert (y == 20);
断言(y == 20);
由于“y”不是原子变量,“y”的存储发生在“x”的存储之前,因此在这种情况下断言不会失败。 The optimizers must still limit the operations performed on shared memory variables around atomic operations.
优化器仍然必须限制围绕原子操作对共享内存变量执行的操作。
Now, what if I make 'y' an atomic variable (without imposing happens-before restrictions)?现在,如果我让 'y' 成为一个原子变量(不强加发生之前的限制)怎么办?
-Thread 1-
y.store (20, memory_order_relaxed);
x.store (10, memory_order_release);
-Thread 2-
if (x.load(memory_order_acquire) == 10)
assert (y.load (memory_order_relaxed) == 20);
Can the assert fail?断言会失败吗? Are there fewer requirements for atomic variables than for non-atomic variables?
对原子变量的要求是否比对非原子变量的要求少? Or is the Wiki's restriction to non-atomic variables gratuitous and misleading here?
或者 Wiki 对非原子变量的限制在这里是无缘无故的和误导性的?
Since 'y' is not an atomic variable, the store to 'y' happens-before the store to 'x'
由于“y”不是原子变量,“y”的存储发生在“x”的存储之前
The statement "since y
is not an atomic" is incorrect.. The same ordering rules apply to atomic and non-atomic operations.语句“因为
y
不是原子的”是不正确的。相同的排序规则适用于原子和非原子操作。
Acquire/release barriers guarantee that memory operation A (the store to y
) sequenced before the store/release happens-before memory operation B (the assert
) sequenced after the load/acquire that sees the stored value.获取/释放屏障保证内存操作 A(存储到
y
)在存储/释放发生之前排序- 在内存操作 B( assert
)之前排序在看到存储值的加载/获取之后。 Whether or not operations A and B are atomic is irrelevant.操作 A 和 B 是否是原子的无关紧要。
The assert
cannot fire. assert
不能触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.