Know that plain loads of atomics compile to ordinary movs on x86
A plain atomic load with any memory order compiles to an ordinary mov on x86-64 with GCC 14, so acquire loads are free and only seq_cst stores cost.
On x86-64 the hardware already orders loads against each other, so GCC 14 does not add a fence to an atomic load, whatever order you ask for. A seq_cst load, an acquire load and a relaxed load all become the same instruction.
#include <atomic>
std::atomic<int> x;
int load_seq() { return x.load(std::memory_order_seq_cst); }
int load_acq() { return x.load(std::memory_order_acquire); }
int load_relaxed() { return x.load(std::memory_order_relaxed); }load_seq():
mov eax, DWORD PTR x[rip]
ret
load_acq():
mov eax, DWORD PTR x[rip]
ret
load_relaxed():
mov eax, DWORD PTR x[rip]
retThis is the assembly from g++ -O2 -S on x86-64. It is why code that is correct only by luck of the x86 memory model passes its tests there and then breaks on arm64, where an acquire load is a different instruction (ldar) and a relaxed one has no ordering at all. Asking for the right order costs nothing here, so ask for it. The expensive case on x86-64 is the seq_cst store, which GCC emits as a mov followed by mfence, or Clang as xchg. The orders are defined in the C++ memory model.