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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

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

ByteSurgeon
Cheat Engine Specialist
MEMBER
Rep: 140
Join Date: Mar 2025
Posts: 19
Thanks: 30
1y ago · Nov 13, 2024 11:06 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?
ByteSurgeon | Cheat Engine & Lua Automation
The following users thanked ByteSurgeon for this post:
SigScannerPro
Pattern Scanning & IDA
MEMBER
Rep: 210
Join Date: Jul 2023
Posts: 25
Thanks: 52
1y ago · Nov 13, 2024 12:38 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.
SigScannerPro · Pattern scanning & Byte masking
LuaScripterX
Luau & Lua VM Reverser
VIP
Rep: 260
Join Date: Mar 2022
Posts: 20
Thanks: 68
1y ago · Nov 13, 2024 1:38 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!
LuaScripterX · Luau VM & Bytecode Engineering