内存模型 release/acquire 宽松原子操作的模式交互
Memory model release/acquire mode interactions of relaxed atomic operations
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-
y = 20;
x.store (10, memory_order_release);
-Thread 2-
if (x.load(memory_order_acquire) == 10)
assert (y == 20);
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. The optimizers must still limit the operations performed on shared memory variables around atomic operations.
现在,如果我将 'y' 设为原子变量(不施加 happens-before 限制)会怎样?
-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);
断言会失败吗?对原子变量的要求是否比对非原子变量的要求少?还是 Wiki 对非原子变量的限制在这里是无端的和误导的?
Since 'y' is not an atomic variable, the store to 'y' happens-before the store to 'x'
语句"since y
is not an atomic"不正确..
相同的排序规则适用于原子操作和非原子操作。
Acquire/release 屏障保证内存操作 A(y
的存储)在 store/release happens-before 内存操作 B 之前排序(assert
)
在看到存储值的 load/acquire 之后排序。操作A和B是否是原子的无关紧要。
assert
无法触发。
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- y = 20; x.store (10, memory_order_release); -Thread 2- if (x.load(memory_order_acquire) == 10) assert (y == 20);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. The optimizers must still limit the operations performed on shared memory variables around atomic operations.
现在,如果我将 'y' 设为原子变量(不施加 happens-before 限制)会怎样?
-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);
断言会失败吗?对原子变量的要求是否比对非原子变量的要求少?还是 Wiki 对非原子变量的限制在这里是无端的和误导的?
Since 'y' is not an atomic variable, the store to 'y' happens-before the store to 'x'
语句"since y
is not an atomic"不正确..
相同的排序规则适用于原子操作和非原子操作。
Acquire/release 屏障保证内存操作 A(y
的存储)在 store/release happens-before 内存操作 B 之前排序(assert
)
在看到存储值的 load/acquire 之后排序。操作A和B是否是原子的无关紧要。
assert
无法触发。