普通的变量读写在编译器 + 处理器的双重优化下行为变得复杂
retry:
if (locked != UNLOCK) {
goto retry;
}
locked = LOCK;
解决方法:编译器和硬件共同提供
for (int i = 0; i < N; i++)
asm volatile("lock incq %0" : "+m"(sum));
“Bus lock”——从 80386 开始引入 (bus control signal)
Acquire/release semantics
x.load()
/x.store()
会根据 memory order 插入 fencex.fetch_add()
将会保证 cst (sequentially consistent)