A Relaxed Guide to memory_order_relaxed
summary
Explains that memory_order_relaxed permits simple load/store code and can be two orders of magnitude faster on weakly ordered CPUs like ARM, but the C++ model permits theoretical out-of-thin-air and RFUB behaviors that complicate static analysis. Gives a pragmatic catalog: safe cases (per-thread counters, single-shared flag, acquire on init / double-checked locking, compare_exchange retry paths), risky cases (speculation causing undefined behavior, lazy init that can write arbitrary addresses, reentrant-mutex tricks). Argues patterns work in practice though standards/compilers need years to catch up.
AI-written summary · about
chapters (20)
- 0:00Intro
- 0:47Overview of the problem
- 3:35Outline of the problem
- 4:32C syntax
- 12:40Outlawing out of thin air
- 13:37C11 handwaving
- 16:01Out of thin air example
- 19:02Known good patterns
- 19:50When is it fully safe
- 20:37Use cases
- 22:35Unidirectional data flow
- 25:18What makes idioms unsafe
- 26:14Question
- 28:28Lazy initialization
- 31:05Reentry mutex
- 32:57Acquisition of mutex
- 37:20Other important examples
- 48:39Summary
- 49:53More Questions
- 54:35Memory Barriers
related talks
01h 02mThe Two Memory Models
01h 03mUsing weakly ordered C++ atomics correctly
01h 01m“C++11/14/17 atomics and memory model..."
01h 28mAtomic’s memory orders, what for?
01h 11mProgramming language memory models: Problems, solutions, and directions
17mPutting Weak Memory in Order via a Promising Intermediate Representation
33mAtomic break down: understanding ordering
01h 28mC++ Memory Model: from C++11 to C++23
