Memory Model
Purpose
Guide agents through C++ and Rust memory models: memory orderings, the happens-before relation, atomic operations, fences, and practical patterns for lock-free data structures.
Triggers
- "What is the C++ memory model?"
- "What memory order should I use for my atomic operation?"
- "What is the difference between acquire-release and seq_cst?"
- "How do I use std::atomic in C++?"
- "What is acquire/release in Rust atomics?"
- "How do I implement a lock-free queue?"
Workflow
1. Memory ordering overview
Modern CPUs and compilers reorder operations for performance. The memory model specifies what reorderings are allowed and how synchronisation is achieved.
2. Memory orderings
3. C++ std::atomic
Acquire-release guarantees: if thread A does a release store to X, and thread B does an acquire load that sees A's value, then all writes by A before the release are visible to B after the acquire.
4. Choosing the right ordering
5. Common patterns
6. Rust atomics
7. Fences
Fences provide ordering without an atomic operation on a specific variable:
8. Common mistakes
For memory ordering rules and happens-before reference, see references/cpp-memory-ordering.md [blocked].
Related skills
- Use
skills/runtimes/sanitizers— TSan detects data races involving non-atomic accesses - Use
skills/rust/rust-sanitizers-mirifor detecting Rust memory ordering violations with Miri - Use
skills/low-level-programming/assembly-x86to understand generated fence instructions - Use
skills/debuggers/gdbfor debugging concurrent programs with thread inspection


