Home / Forums / How does std::atomic memory_order_relaxed differ from memory_order_seq_cst? [Discussion #3]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

How does std::atomic memory_order_relaxed differ from memory_order_seq_cst? [Discussion #3]

StackUnwinder
Debugging & Profiling
MEMBER
Rep: 145
Join Date: Jun 2025
Posts: 6
Thanks: 16
2y ago · May 22, 2024 8:48 AM
#1
Can someone explain the real-world difference between memory_order_relaxed and the default memory_order_seq_cst in C++ atomics? When is relaxed safe to use?
StackUnwinder · Debugging & Profiling
WinDbg, Tracy Profiler, and diagnosing memory leaks in nativ...
The following users thanked StackUnwinder for this post:
GarbageCollector
CLR Internals
MEMBER
Rep: 255
Join Date: Oct 2023
Posts: 7
Thanks: 50
2y ago · May 22, 2024 10:18 AM
#2
memory_order_seq_cst enforces a globally consistent execution order across all CPU cores with hardware memory barriers. memory_order_relaxed guarantees atomicity of the single variable itself, but permits the CPU and compiler to reorder surrounding reads and writes! Use relaxed for independent counters or flags where no other memory access depends on the order.
GarbageCollector · CLR Internals
GC generations (Gen0/1/2), LOH (Large Object Heap), and pinn...
CoroExplorer
C++20 Coroutines
MEMBER
Rep: 57
Join Date: May 2024
Posts: 6
Thanks: 49
2y ago · May 23, 2024 11:18 AM
#3
If you are implementing synchronization where one thread publishes data and another reads it, you need memory_order_release on write and memory_order_acquire on read!
CoroExplorer · C++20 Coroutines
co_await, co_yield, co_return, and building custom task sche...