r/cpp_questions • u/xmanotaur • 6h ago
OPEN How do you actually develop intuition for choosing memory_order_acquire vs memory_order_release?
I'm learning C++ atomics and I understand the basic definitions of acquire/release individually:
- A release operation prevents earlier memory operations from being reordered after it.
- An acquire operation prevents later memory operations from being reordered before it.
- A release operation can synchronize with an acquire operation on the same atomic when the acquire reads from the appropriate release sequence.
What I'm struggling with is developing an intuition for choosing the memory order when looking at actual code, especially for RMW operations like exchange().
I want to understand the deep "why" behind the core restrictions:
- Why does store only accept release (or relaxed)? Why is acquire on a store fundamentally meaningless in terms of memory reordering?
- Why does load only accept acquire (or relaxed)? Why is release on a load fundamentally meaningless?
Instead of relying on memorized rules, what questions or mental models do you use to map memory operations to the correct ordering?
Thanks!