Concepts and theorems · Intermediate

Memory barriers: specify ordering between memory accesses

Separate atomic access from ordering and analyse publication using a data-and-flag example.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Atomicity is not ordering
  2. Publish data with a synchronization pair
  3. Ordering does not provide mutual exclusion
  4. Start from a property to establish
  5. Trace one-time publication
  6. Treat device ordering as its own contract
  7. Ask Logeia about this articleCheck your understanding

Atomicity is not ordering

A memory barrier expresses constraints on the ordering of memory accesses. An indivisible access to one object does not automatically establish how operations on another object are observed. Both compiler transformations and the processor’s execution and visibility rules matter. Language-level atomic operations, operating-system kernel barriers and device-I/O barriers are not interchangeable tools with one universal meaning. State which memory model and which observers the rule concerns. This prevents a hardware instruction name from being used as a substitute for the synchronization contract required by the source language.

Source-code order is not the complete ordering contract. Explain communication through executions permitted by the language or API being used. Copying a Linux kernel macro into a user-space C++ program does not establish the synchronization relation required by the C++ standard. Begin with the contract under which the program is claimed to be correct, then examine the emitted machine instructions. An implementation detail observed on one processor does not replace the portable reasoning required by a different programming model.

Publish data with a synchronization pair

Consider a publisher that writes data and then announces readiness, and a reader that observes readiness before using the data. A suitable release/acquire pair can establish publication when the language’s synchronization conditions hold. Ordinary assignments or relaxed operations on the flag do not automatically provide the same ordering. The example also needs lifetime and ownership rules: if the publisher continues modifying the data without coordination after publication, another race can remain. A one-time handoff is easier to reason about than an unspecified reusable flag, so make the intended protocol explicit before choosing memory-order operations.

For a one-time publication, the important fact is that the acquire load reads the value written by the relevant release store. Merely saying that a flag equals one can become ambiguous when another writer exists or the flag is reused for a later round. A reusable buffer needs another rule establishing when the next mutation may begin. Publishing completed initialization is not the same problem as coordinating ongoing updates to shared mutable data. State which operation the reader observes and who owns the payload afterwards.

Ordering does not provide mutual exclusion

Inserting a barrier into two competing read-add-write sequences does not turn them into one atomic update. Mutual exclusion or an appropriate atomic read-modify-write operation is a separate requirement. A barrier also should not be pictured as necessarily flushing every cache or stopping all CPUs together. Different architectures can implement the required ordering differently, and some ordering operations need no additional machine instruction in a given setting. Reason from the promised ordering and atomicity first, then inspect the implementation. Otherwise a superficially powerful barrier can leave the actual data race untouched.

If two workers read a balance of zero, add one, and write one, ordering constraints alone do not make the result two. Use a suitable atomic read-modify-write operation or protect the whole sequence with a lock. Freedom from data races, absence of lost updates, and publication of another object are distinct properties. Listing them separately explains why adding more fences can leave an algorithm incorrect. An atomic counter can count events correctly while still requiring additional synchronization for payload data associated with those events.

Start from a property to establish

Write down who reads and writes each object and which observed outcome must be excluded. If ordinary shared variables have a data race in C or C++, adding only a processor barrier is not a general repair for source-language undefined behaviour. Use the language’s atomic or locking facilities with a valid protocol. A stress test that never observes a failure does not prove correctness for every permitted execution. Model descriptions, careful reasoning and, where suitable, small litmus tests or model checking provide complementary evidence. Tests are useful, but their absence of failures does not create a missing ordering guarantee.

For a specific example, initialize atomic x and y to zero. One thread stores one to x and then reads y; another stores one to y and then reads x. With all operations relaxed, the result in which both reads observe zero is not automatically forbidden. The same small program with every operation sequentially consistent excludes that outcome. Every access being atomic therefore does not establish the expected ordering across multiple variables. Separate what the model permits from how frequently a particular processor and compiler happen to exhibit it.

Trace one-time publication

Consider a single writer that initializes a payload, then stores true to an atomic ready flag with release ordering. A reader waits until an acquire load observes that publication and then reads the payload. The argument requires that the observed flag comes from the corresponding publication, the payload remains alive, and no uncontrolled mutation follows. If the writer immediately reuses the payload for another request, the original publication relation does not protect the reader from that later write. Resetting the flag to false also does not tell the writer that the reader has finished using the object. A repeated handoff requires an acknowledgement or ownership protocol. Identify both the publication event and the lifetime during which the published data can be used.

Treat device ordering as its own contract

A device driver may write a descriptor into memory and then ring a device doorbell through an MMIO register. Here the relevant observer includes a device, not merely another C++ thread. Thread synchronization alone does not explain DMA visibility, MMIO ordering, or when the device has consumed a buffer. Use the kernel’s accessor and DMA interfaces according to their platform-specific contracts. Distinguish a CPU store, device visibility, and device completion. Choosing one instruction observed on one architecture is not a general driver recipe: memory types, coherent or noncoherent DMA, and the device interface affect the required operations. This is why language atomics, kernel barriers, and device accessors must remain separate abstractions.

Reading and practice are recorded separately in this browser, for each language.

Choose a learning path