Sub topic 14
Why acquire and release ordering is needed
This chapter assumes you have read the previous chapter on the lock-free single-producer, single-consumer (SPSC) queue. It also assumes you know what a thread is and have basic C. You don't need prior knowledge of the C11 memory model. This chapter introduces what you need.
The problem: reordering
The SPSC queue has no lock. The producer writes a value into a slot, then increments tail. The consumer sees the new tail, then reads the slot. That order looks obvious in the source code, but neither the compiler nor the CPU promises to keep it.
Compilers and CPUs reorder memory operations that seem independent, because doing so is faster. Store buffers and caches can also make one core see writes from another core in a different order than they were issued. On a single thread you never notice, because the result is the same. With two threads, the consumer can see the new tail before it sees the data written to the slot. It then reads a stale value.
Here is a broken push that uses relaxed ordering everywhere:
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#define CAP 8u
typedef struct {
int data[CAP];
_Atomic uint32_t head;
_Atomic uint32_t tail;
} spsc_t;
bool broken_push(spsc_t *q, int v) {
uint32_t t = atomic_load_explicit(&q->tail, memory_order_relaxed);
uint32_t h = atomic_load_explicit(&q->head, memory_order_relaxed);
if (t - h == CAP) return false;
q->data[t & (CAP - 1u)] = v;
/* relaxed: the slot write may become visible AFTER this store */
atomic_store_explicit(&q->tail, t + 1u, memory_order_relaxed);
return true;
}A relaxed operation is atomic, so the index is never torn. It gives no ordering guarantee relative to other memory operations.
What release and acquire guarantee
The fix is a pair of operations that work together.
- A release store guarantees that every write the thread made before it is visible to any thread that reads the stored value with an acquire load.
- An acquire load guarantees that every read and write after it sees what the releasing thread wrote before its release store.
Together they form a one-way handoff. In spsc_push, the producer writes the slot and then does atomic_store_explicit(&q->tail, t + 1u, memory_order_release). In spsc_pop, the consumer does atomic_load_explicit(&q->tail, memory_order_acquire). If the consumer sees the new tail, it is guaranteed to see the slot contents.
The same pairing works in the other direction. The consumer releases head after it finishes reading a slot. The producer acquires head before it overwrites that slot. This stops the producer from writing into a slot the consumer is still reading.
Why each index uses these orderings
| Operation | Ordering | Reason |
|---|---|---|
| Producer loads its own `tail` | relaxed | Only the producer writes `tail`, so it reads its own latest value. |
| Producer loads `head` | acquire | Pairs with the consumer's release, so the slot is really free. |
| Producer stores `tail` | release | Publishes the slot data to the consumer. |
| Consumer loads its own `head` | relaxed | Only the consumer writes `head`. |
| Consumer loads `tail` | acquire | Pairs with the producer's release, so the data is visible. |
| Consumer stores `head` | release | Tells the producer the slot has been read. |
Sequentially consistent ordering (memory_order_seq_cst) would also be correct. It is the default when you omit the _explicit forms. It adds a global ordering that this queue does not need, and it can cost more on some CPUs.
Common mistakes
- Using plain
uint32_tindices instead of_Atomic. Concurrent access to a non-atomic variable is a data race, which is undefined behavior in C. - Using relaxed ordering on the publishing store. The code passes tests on x86 and fails on ARM, where reordering is more visible.
- Putting the slot write after the release store. Only writes before the release are published.
Exercise
In spsc_pop, the consumer loads tail with acquire. What can go wrong if it loads tail with relaxed instead?
Show answer
The consumer could see the new tail value but still read a stale slot, because nothing orders the slot read after the tail load. It might pop garbage or an old value. The acquire load pairs with the producer's release store and makes the slot data visible.