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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

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

SecurityAudit
AppSec & Code Auditor
MEMBER
Rep: 135
Join Date: Jan 2025
Posts: 28
Thanks: 29
5y ago · Aug 4, 2021 12:51 PM
#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?
SecurityAudit | Secure C++ Architecture & Boundary Auditing
The following users thanked SecurityAudit for this post:
KeycardMaster
Item ESP & Unity UI Dev
MEMBER
Rep: 215
Join Date: Oct 2023
Posts: 19
Thanks: 54
5y ago · Aug 4, 2021 2:26 PM
#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.
KeycardMaster | Keycard & Objective ESP Specialist
NativeCoder
Low-Level C++ / ASM
VIP
Rep: 374
Join Date: Jun 2025
Posts: 12
Thanks: 20
5y ago · Aug 4, 2021 5:26 PM
#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!
NativeCoder · Low-Level C++ / ASM
Passionate about cache locality, SIMD instructions, and raw ...