Skip to main content
Definition Website Computer and Technology

Memory Barrier

Subject: Computer and Technology noun /ˈmɛm.ər.i ˈbɛr.i.ər/ 1 definition

  1. Definition (noun)

    Entry #347 · Updated

    A Memory Barrier (also known as a memory fence) is a CPU instruction or compiler directive that enforces strict ordering constraints on memory operations—specifically loads and stores—issued before and after the barrier point. Modern optimizing compilers frequently reorder instructions or cache variables in registers, while multi-core processors rely on out-of-order execution, store buffers, and cache-coherency invalidation queues that can cause other cores or peripheral devices to observe memory updates out of program order. When encountered, a memory barrier restricts compiler code motion across the boundary and—when backed by a hardware fence instruction—flushes pending store buffers, processes queued cache invalidations, or stalls subsequent memory accesses until earlier operations become globally visible under the architecture's memory consistency model. While indispensable for lock-free data structures, kernel synchronization primitives, and memory-mapped I/O (MMIO) device drivers—especially on weakly ordered architectures such as ARM and POWER—a memory barrier guarantees relative ordering and cross-core visibility rather than mutual exclusion or multi-step atomicity, and excessive fencing reduces instruction-level parallelism.

    Common Types

    • Full Memory Barrier: Blocks both loads and stores from being reordered across the boundary in either direction, enforcing strict bidirectional ordering via instructions such as x86 MFENCE, ARM DMB SY, or C++ std::atomic_thread_fence(std::memory_order_seq_cst).

    • Read (Load) and Write (Store) Barriers: Constrain ordering in a single operation direction; a read barrier (such as Linux rmb() or ARM DMB LD) ensures earlier loads complete before later loads, whereas a write barrier (such as x86 SFENCE or Linux wmb()) ensures earlier stores become visible before later stores.

    • Acquire and Release Barriers: Provide asymmetric, half-fence ordering commonly used in lock-free handoffs—an acquire barrier prevents subsequent reads and writes from moving before an associated load, while a release barrier prevents prior reads and writes from moving after an associated store (such as ARM64 LDAR and STLR).

    • Compiler Barrier: Instructs the compiler not to reorder memory accesses or retain stale values in CPU registers across the directive without emitting any hardware fence instruction, such as GCC/Clang's asm volatile("" ::: "memory") or C++ std::atomic_signal_fence, making it useful for single-core interrupt safety or pairing with CPU fences.

    Example

    In a C++ lock-free ring buffer on a multi-core ARM64 processor, the producer thread writes a 64-byte telemetry payload into shared memory and executes std::atomic_thread_fence(std::memory_order_release) before publishing the updated tail index via a std::memory_order_relaxed atomic store. A consumer thread on another core loads tail with std::memory_order_relaxed followed by std::atomic_thread_fence(std::memory_order_acquire), preventing the compiler or CPU from speculatively reading stale payload bytes before the producer's writes are visible.