memory consistency model relaxed
**Memory Consistency Models** define **the rules governing when stores performed by one processor become visible to loads performed by other processors — establishing the contract between hardware and software that determines which reorderings of memory operations are permitted and which synchronization primitives programmers must use to enforce ordering**.
**Consistency Model Spectrum:**
- **Sequential Consistency (SC)**: all processors observe the same total order of all memory operations, and each processor's operations appear in program order within that total ordering — simplest to reason about but most restrictive for hardware optimization
- **Total Store Order (TSO)**: stores may be buffered and reordered after later loads (store-load reordering), but all processors observe stores in the same order; x86/x86-64 implements TSO — permits store buffers while maintaining strong consistency for most programs
- **Relaxed Consistency**: both loads and stores may be reordered freely by hardware for maximum performance; ARM, RISC-V, POWER implement relaxed models — programmers must use explicit fence instructions or atomic operations with ordering constraints to enforce visibility
- **Release Consistency**: distinguishes acquire operations (loads that prevent subsequent operations from moving before them) and release operations (stores that prevent prior operations from moving after them) — provides ordering at synchronization points without constraining ordinary accesses
**Memory Ordering Primitives:**
- **Memory Fences/Barriers**: explicit instructions that prevent reordering across the fence; full fence (mfence on x86, dmb ish on ARM) prevents all reordering; lighter-weight fences (dmb ishld for loads only) provide partial ordering at lower cost
- **Atomic Operations**: load-acquire atomics prevent subsequent operations from being reordered before the load; store-release atomics prevent prior operations from being reordered after the store; combining acquire-load and release-store creates a synchronization pair
- **Compare-and-Swap (CAS)**: atomic read-modify-write with sequential consistency semantics (on most architectures); serves as both synchronization point and atomic data modification — the building block of lock-free algorithms
- **Compiler Barriers**: prevent compiler reordering independently of hardware fences; volatile in C/C++ prevents optimization of specific variables; std::atomic with memory_order provides both compiler and hardware ordering
**Practical Impact:**
- **Lock-Free Algorithms**: must use appropriate memory ordering to ensure correctness; the classic double-checked locking pattern requires acquire-release semantics on the flag variable — without proper ordering, another thread may see the initialized flag but stale data
- **Performance vs Correctness**: stronger ordering (sequential consistency) is safer but prevents hardware optimizations; relaxed ordering enables out-of-order execution and store buffer optimizations but risks subtle bugs; the right choice depends on the specific algorithm
- **Architecture Portability**: code correct on x86 (TSO) may break on ARM (relaxed) because x86 implicitly provides store-load ordering that ARM does not; portable concurrent code must use explicit atomic operations with specified memory order
- **Testing Difficulty**: memory ordering bugs are inherently non-deterministic; they manifest only under specific timing conditions on specific hardware; litmus tests and model checkers (herd7, CppMem) systematically verify ordering properties
Memory consistency models are **the fundamental contract underlying all concurrent programming — understanding the difference between sequential consistency, TSO, and relaxed ordering is essential for writing correct lock-free code, debugging subtle concurrency bugs, and achieving maximum performance on modern multi-core and heterogeneous architectures**.