Section 9.11

Acquire-release ordering

Definition

Acquire-release ordering is a pairing rule for memory operations, defined in the C11 and C++11 language standards. A store (write) with release order publishes every earlier memory write of its thread to any thread whose load (read) with acquire order reads the stored value. The release keeps all earlier memory accesses of its thread before it; the acquire keeps all later accesses of its thread after it. Unlike sequential consistency §9.12, it orders each matched pair, not all operations in one global order.

The idea

A writer thread, one of several instruction sequences sharing one memory, builds a record and hands it to a reader thread through a shared pointer:

p->id = 7; p->length = 4; slot = p; writer
q = slot; if (q) use(q->id, q->length); reader

The writer means the record to be ready when slot changes, so it stores the pointer last. Nothing in plain C keeps that meaning. The compiler, or the processor running the writer, may make the store to slot visible to other cores before the two field writes. The reader can then follow a valid pointer to fields that still hold old values.

Acquire-release ordering attaches the meaning to the two operations that carry it. The writer’s release store marks everything the writer wrote before it as part of the message. The reader’s acquire load makes everything the reader does after it see that message, provided the load read the value the release stored. A sealed envelope is a fair picture: the writer puts the record in and seals the envelope (release), and a reader who opens that envelope (acquire) finds everything put in before sealing. The picture stops in two places. The guarantee holds only for a reader who opened that particular envelope, meaning a load that read the value that release stored. And sealing does not stop the writer from changing the record afterwards; the protocol must forbid that separately.

How it works

Each order restricts reordering within its own thread, by the compiler and by the processor:

Each is a one-way fence. A release keeps earlier accesses in but lets later ones move up across it; an acquire keeps later accesses in but lets earlier ones move down across it. The standard states the effect as the synchronizes-with rule given below; these reordering rules are how compilers and processors implement it.

Safe publication of an object uses five steps.

  1. The writer allocates the object and initializes every field while no other thread can reach it.

  2. The writer stores the object’s address into a shared atomic pointer with release order.

  3. The reader loads the atomic pointer with acquire order.

  4. If the reader obtained the new address, the release synchronizes with the acquire, and by the happens-before relation §9.10 every write the writer made before step 2 is ordered before every access the reader makes after step 3. The reader reads the fields and sees the values from step 1.

  5. The writer does not modify the fields after step 2, and nobody frees the object while a reader may still use it. Neither follows from the ordering; immutable publication §9.24 and object lifetime §4.1 rules supply them.

Processors implement the pair cheaply. Armv8-A processors have dedicated instructions, STLR (store-release) and LDAR (load-acquire). On Armv7 processors compilers place a DMB barrier instruction before a release store and after an acquire load. On x86 processors ordinary loads and stores already behave as acquire and release, so only the compiler needs to be restrained.

Worked example

A record has two fields, id and length, and an atomic pointer slot starts as the null pointer §4.26. The writer runs

p->id = 7;
p->length = 4;
atomic_store_explicit(&slot, p, memory_order_release);

and the reader runs

q = atomic_load_explicit(&slot, memory_order_acquire);
if (q != NULL) { a = q->id; b = q->length; }

Trace an execution in which the load returns p.

  1. The two field writes come before the release store in the writer’s program order, so the release keeps them before it.

  2. The acquire load read p, the value the release store wrote, so the store synchronizes with the load.

  3. The two field reads come after the acquire load in the reader’s program order.

  4. So each field write happens before each field read, and a=7a = 7, b=4b = 4. No other result is allowed.

In an execution in which the load returns the null pointer, the reader skips the fields, and nothing needs ordering.

Replace the reader’s acquire by memory_order_relaxed. The load is still atomic: q is either the null pointer or p, never a mixture of their bits. But a relaxed load is not an acquire, so the store no longer synchronizes with it. The field writes and the field reads are then non-atomic accesses to the same memory in different threads with no happens-before between them, which the C standard calls a data race §9.3. The program has undefined behavior §4.12. Reading the old values 0 and 0 is only one of the outcomes the standard no longer excludes.

Acquire-release ordering does not order everything. Let x and y be atomic integers that start at 0.

Thread A: store 1 to x with release; then r1r_1 = load of y with acquire.
Thread B: store 1 to y with release; then r2r_2 = load of x with acquire.

The outcome r1=0r_1 = 0 and r2=0r_2 = 0 is allowed. A release followed by an acquire of a different location may be reordered, because the release only keeps earlier accesses before it and the acquire only keeps later accesses after it. On x86 hardware the outcome occurs in practice: each core’s store waits in its store buffer, a queue of writes not yet visible to other cores, while its later load runs. With sequentially consistent operations on both sides, r1=r2=0r_1 = r_2 = 0 is impossible.

Stated precisely

Let WW be an atomic store with release order, or a stronger order, to an atomic object MM, performed by thread A. Let RR be an atomic load of MM with acquire order, or stronger, performed by thread B. If RR reads the value that WW wrote, or a value written by a read-modify-write that follows WW in MM’s modification order (the single order of all writes to MM) with only read-modify-writes in between, then WW synchronizes with RR. Every evaluation that comes before WW in A’s program order then happens before every evaluation that comes after RR in B’s program order. C11 counts such a chain of read-modify-writes as part of the release sequence headed by WW.

Publication through MM is then correct under four conditions.

  1. MM is an atomic object, and both operations act on MM.

  2. RR read the value WW wrote, or one from its release sequence; the reader must check what it read.

  3. No thread writes the published fields after WW unless that write is itself ordered with the reads, for example by a further release and acquire pair or by a lock.

  4. The object stays alive for as long as any reader may use it.

Memory order supplies neither condition 3 nor condition 4.

Limits and common mistakes

In the two-operation publication example, replacing either the release store or the acquire load with a relaxed operation removes the synchronization edge. A separate relaxed read-modify-write can still relay an earlier release. If it increments a published flag from 1 to 2 in that release sequence, an acquire load that reads 2 synchronizes with the original release.

Each release orders the work it publishes for a reader whose acquire reads from that release or its release sequence. If two threads publish through separate atomic variables, different readers can observe the publications in different orders. When all participants need one common order, they can put the relevant operations under the same mutex §9.5, or use sequentially consistent atomic operations §9.8 for all of them.

Ordering is not lifetime. A writer that frees the old record right after publishing a new one causes a use-after-free §4.6 in every reader still using the old record. Read-copy update (RCU) §9.20, reference counting §9.22 or a lock supplies the lifetime guarantee.

Republishing a changed object is not covered. If the writer modifies the fields after the release and releases the same pointer again, the reader’s field reads race with the new writes, which is undefined behavior, and in practice a reader can see a mixture of old and new fields. Publish a new object that is never modified, or protect the fields with a lock.

C11 also defines consume ordering, a weaker acquire meant to order only accesses that depend on the loaded pointer. Compilers implement it as acquire. Relying on such dependencies after a relaxed load is not a substitute.

Fences, standalone ordering operations written atomic_thread_fence in C, can carry the orders instead of the load and store: a release fence before a relaxed store pairs with an acquire fence after a relaxed load that reads the stored value.

Questions answered

What changes if the reader performs only a relaxed load?

With only a relaxed load, the reader still reads the pointer or flag atomically, but the publication guarantee disappears: the writer’s earlier writes to the object are no longer ordered before the reader’s reads of it. Acquire-release ordering works only as a pair. A release store by the writer synchronizes with an acquire load by the reader that reads the stored value, and that link makes every write the writer made before the store visible to everything the reader does after the load. A relaxed load is atomic, so it returns a whole value, the old one or the newly published one, but it is not an acquire, so no link forms even when it reads the new value.

The reader’s ordinary reads of the object’s fields and the writer’s initializing writes are then conflicting accesses, accesses to the same memory with at least one write, in different threads with no happens-before relation between them, which C and C++ define as a data race. The program has undefined behavior, which is worse than merely reading stale values. In practice, when the reader checks a flag and then reads separate data, a weakly ordered processor, such as a multi-core Arm processor, can perform the data read early and return the old value. When the reader reads the fields through the loaded pointer, most processors keep the order because the field addresses depend on the pointer, so the bug can stay hidden until a compiler optimization §3.39 or a different processor exposes it. The repair is an acquire load, or a relaxed load followed by an acquire fence (atomic_thread_fence with memory_order_acquire) before the field reads.

Related entries: Happens-before relation (§9.10), Immutable publication (§9.24), Read-copy update (RCU) (§9.20), Object lifetime (§4.1), Memory barrier (§4.37), Sequential consistency (§9.12). Further reading: [78].