
今天看到 atomic 相关的东西,然后就顺到了关于 memory_order 的东西
网上说下面的代码有可能会出现 r1 == 1111 && r2 == 1111 的情况,我运行了几万遍了,还是没碰到。还是说只是理论上会出现?大佬们怎么看?
#include <iostream> #include <thread&g; #include <atomic> using namespace std; atomic_int x {0}; atomic_int y {1}; void func1() { int r1 = y.load(std::__1::memory_order_relaxed); x.store(r1, std::__1::memory_order_relaxed); } void func2() { int r2 = x.load(std::__1::memory_order_relaxed); y.store(1111, std::__1::memory_order_relaxed); } int main() { thread t1(func1); thread t2(func2); t1.join(); t2.join(); } 1 codehz 2021-03-05 20:45:18 +08:00 在 apple silicon 上就有效果了,intel 强制严格内存序 |
2 dinghao188 OP @codehz 原来是这样,改天试下 |