What is Memory barrier?
An instruction guaranteeing that memory operations before it become visible to other cores before those after it, also called a fence, such as RISC-V fence.
Japanese name: メモリバリア
An instruction guaranteeing that memory operations before it become visible to other cores before those after it, also called a fence, such as RISC-V fence.
Japanese name: メモリバリア
A memory barrier, also called a fence, is an instruction that splits a thread's memory operations into those before it and those after it, and guarantees that the earlier ones become visible to other cores before the later ones are performed. This describes a full barrier that orders all loads and stores; some kinds order only selected operations, such as loads with loads or stores with stores. Without it, a processor may let loads and stores to different addresses complete out of order, for example by keeping stores in a store buffer while later loads proceed. Results seen by the issuing thread itself never change, so barriers matter only when data is shared between cores or with devices such as DMA engines.
Suppose core 0 runs x = 1 and then r1 = y, while core 1 runs y = 1 and then r2 = x, with x and y starting at 0. Under sequential consistency one store happens first, so at least one load must return 1. On real processors, including x86, each store can wait in its core's store buffer while the following load reads the old value from cache, so both r1 and r2 can be 0. Placing a full barrier between the store and the load in each thread forces the store to become visible before the load runs, which rules out that outcome.
RISC-V provides fence, whose operands state which earlier accesses must be ordered before which later ones, so fence rw, rw orders all reads and writes, and its atomic and load-reserved/store-conditional instructions carry acquire and release bits. Arm has dmb (data memory barrier) for ordering, dsb when completion must be awaited, and load-acquire and store-release instructions. On x86, whose total store order (TSO) already preserves most orderings, mfence or a locked instruction is needed mainly to order a store before a later load. Barriers are not free, because they can stall the core until pending stores drain.
Most programmers never write a barrier directly. Lock acquisition includes acquire semantics, so later accesses cannot move before it, and release includes release semantics, so earlier writes are visible before the lock is freed; this is why data protected by a lock passes correctly between threads. Languages expose the same ideas through atomics with memory orders, such as C11 and C++11. A compiler barrier is a different thing: it stops the compiler from reordering accesses across a point but emits no instruction, so it does not constrain the hardware.
Answer: A. It guarantees that memory operations before the barrier become visible to other cores before those after it
CPUs can reorder memory operations, so passing data to other cores in a fixed order requires an instruction that prevents it. A full memory barrier completes all earlier reads and writes, making the writes visible to other cores, before later ones are performed. There are also lighter barriers that order only selected kinds of reads and writes. Lock acquisition and release include this barrier effect, which is why variables protected by a lock are passed correctly.
They are the same concept under two names. Both refer to an instruction that keeps memory operations before it ordered ahead of those after it as observed by other cores; RISC-V calls its instruction fence, and Arm calls its instructions barriers.
Less often than weakly ordered architectures, because x86 total store order keeps most loads and stores in program order. A barrier such as mfence, or a locked instruction, is still needed when a store must become visible before a later load to a different address.
A compiler barrier only prevents the compiler from moving memory accesses across it and generates no machine instruction. A memory barrier is a hardware instruction that constrains the order in which the processor makes accesses visible to other cores.
Memory barrier appears in 2 practice questions across 1 topic, including Multicore & Synchronization.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.