</>EmbeddedInterview

What is acquire and release ordering?

MediumARM Architecture

Short answer: Release orders earlier accesses before publication; acquire orders later accesses after observing publication. Together they support safe producer-consumer communication.

ProducerWrite dataRelease / AcquirePublish flagConsumerRead data
STLR W0, [X1] // release store
LDAR W2, [X1] // acquire load

1. Producer: Write the payload first, then publish the flag with release ordering.

2. Consumer: Load the flag with acquire ordering. If it observes the release publication, subsequent reads can safely access the payload in this one-time publication pattern.

// Linux kernel; initially flag = 0
WRITE_ONCE(data, 100);
smp_store_release(&flag, 1);

// Other CPU
if (smp_load_acquire(&flag))
    value = READ_ONCE(data);

3. Difference from full barriers: Release primarily orders preceding accesses before publication; acquire orders following accesses after observation. A full barrier is stronger in other ordering directions.

Interview takeaway: Acquire/release is a common efficient building block for locks, queues and producer-consumer synchronization.

C11 example and ARM64 lowering

#include <stdatomic.h>
static int payload;
static atomic_int published;

void send_once(void)
{
    payload = 42;
    atomic_store_explicit(&published, 1, memory_order_release);
}

int receive(void)
{
    if (atomic_load_explicit(&published, memory_order_acquire))
        return payload;
    return -1;
}

When the acquire load reads the release publication, the payload write happens-before the consumer's payload read. On AArch64, a suitable compiler may implement the release store with STLR and the acquire load with LDAR or an equivalent instruction sequence.

For a standalone statistics counter, relaxed atomics can be sufficient because no other data is published. For a lock, acquisition uses acquire semantics and unlock uses release so protected writes become visible to the next owner. Sequentially consistent atomics impose additional ordering requirements. Acquire/release is directional rather than a full fence in both directions. The example is one-time publication: reuse of payload requires acknowledgment or another ownership protocol.