根据 GCC 定义:https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
__ATOMIC_SEQ_CST 强制执行所有其他 __ATOMIC_SEQ_CST 操作的总排序。
现在让我们看下面的例子:
int test(int* num)
{
int m = __atomic_load_n(num, __ATOMIC_SEQ_CST);
int z = __atomic_load_n(num, __ATOMIC_SEQ_CST);
}
使用 x86-64 gcc 12.2 编译使用此站点生成以下程序集https://godbolt.org/
test:
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov rax, QWORD PTR [rbp-24]
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-4], eax
mov rax, QWORD PTR [rbp-24]
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-8], eax
nop
pop rbp
ret
现在我的问题是,是什么阻止了 CPU 在这两个原子操作之间重新排序?