Developer knowledge network · moderated exchange

UnreliableCode қауымдастығы

Әзірлеушілерді зерттеу, кері инженерия және кодтау қауымдастығы

Knowledge indexТірі
4Categories
919Threads
2.8KЖазбалар
Discussion

Understanding std::memory_order_acquire and std::memory_order_release in lock-free queues [StackOverflow Architecture Guide]

cpp_concurrency_guru
C++ Standards Expert
MEMBER
Өкіл: 47
Қосылу күні: Feb 2018
Хабарламалар: 17
Рахмет: 75
2 ай бұрын · Jun 25, 2026 3:37 AM
#1

Why std::memory_order_seq_cst is often overkill for single-producer single-consumer queues:

When writing a lock-free queue:

  • Producer: Writes payload to buffer, then performs tail.store(newTail, std::memory_order_release).
  • Consumer: Loads tail.load(std::memory_order_acquire), then reads payload.

release guarantees that all prior memory writes (the buffer data) are visible to any thread that executes an acquire load on that same atomic variable. On x86/x64, hardware already enforces TSO (Total Store Order), so acquire/release loads/stores compile to plain mov instructions with zero mfence penalty!

memory_model_mook
Low-Level C Veteran
MEMBER
Өкіл: 163
Қосылу күні: Jan 2019
Хабарламалар: 11
Рахмет: 33
1 ай бұрын · Jun 25, 2026 10:10 AM
#2

Great explanation. On ARM64 (which is weakly ordered), acquire generates LDAR and release generates STLR instructions, avoiding full DMB ISH memory barriers.

profiler_pat
Performance Hunter
MEMBER
Өкіл: 146
Қосылу күні: Aug 2019
Хабарламалар: 33
Рахмет: 31
1 ай бұрын · Jun 25, 2026 4:43 PM
#3

Lock-free algorithms without acquire-release semantics are a ticking time bomb on modern mobile and server processors.