skip to content

In a C++ single-producer single-consumer audio ring buffer, why must the head index be stored with release and loaded with acquire, and what breaks if both are relaxed?

level: seniorimportance: should knowfreq 55%

answer

  1. the index publishes the slot
  2. release store, acquire load pair
  3. synchronizes-with, then happens-before
  4. relaxed index races the plain buffer

basics

~20 s

The producer writes the sample into a plain buffer slot, then stores head with release; the consumer loads head with acquire. That pairing makes the slot write happen before the consumer's read. With relaxed, the buffer access is a data race.

solid answer

~50 s

The samples live in a plain array; only the head and tail indices are atomic. The producer writes `buf[h % N]` and then `head.store(h + 1, memory_order_release)`. The consumer does `head.load(memory_order_acquire)` and, if it sees `h + 1`, reads the slot. A release store **synchronizes with** an acquire load that reads the value it wrote, so everything the producer wrote before the store **happens before** everything the consumer does after the load — the sample is guaranteed visible. The tail index works the same way in the other direction, so the producer cannot overwrite a slot still being read. Make both relaxed and the indices are still atomic, but nothing orders the plain buffer access with the other thread's: that is a data race, undefined behavior, and ThreadSanitizer reports it. Each thread's own index can be loaded relaxed, because only that thread writes it.

code

cpp · 32 lines
cpp
#include <array>
#include <atomic>
#include <cstddef>
#include <cstdio>
#include <thread>
template <std::size_t N> class SpscRing {
    std::array<float, N> buf_{};
    std::atomic<std::size_t> head_{0}, tail_{0};  // head: producer, tail: consumer
public:
    bool push(float s) {
        std::size_t h = head_.load(std::memory_order_relaxed);
        if (h - tail_.load(std::memory_order_acquire) == N) return false;  // full
        buf_[h % N] = s;                                   // plain write
        head_.store(h + 1, std::memory_order_release);     // publish the slot
        return true;
    }
    bool pop(float& out) {
        std::size_t t = tail_.load(std::memory_order_relaxed);
        if (head_.load(std::memory_order_acquire) == t) return false;     // empty
        out = buf_[t % N];                                 // sees the plain write
        tail_.store(t + 1, std::memory_order_release);     // hand the slot back
        return true;
    }
};
int main() {
    SpscRing<256> ring;
    std::thread audio([&] { for (int i = 0; i < 100000; ++i) while (!ring.push(i)) {} });
    double sum = 0;
    for (int got = 0; got < 100000;) { float s; if (ring.pop(s)) { sum += s; ++got; } }
    audio.join();
    std::printf("sum=%.0f\n", sum);  // 4999950000
}

go deeper

for a junior

Recall that a release store and an acquire load of the same atomic let one thread safely hand plain data to another.

for a middle

Walk through the four steps of the handshake and say which loads can be relaxed because only one thread writes that index.

for a senior

Explain why the relaxed variant is a data race even when it passes tests on x86-64, and show the sanitizer and codegen evidence.

for a principal

Weigh whether a hand-written SPSC ring belongs in the codebase at all versus a reviewed library queue, given how few people can verify its ordering.

## The design A **single-producer single-consumer (SPSC)** ring buffer moves audio samples from a decoder thread to the audio callback without a mutex, so the real-time thread never blocks. It has three parts: - `buf_` — a plain `std::array<float, N>`; samples are written and read with ordinary loads and stores. - `head_` — a `std::atomic<std::size_t>` written **only by the producer**: the number of slots filled so far. - `tail_` — a `std::atomic<std::size_t>` written **only by the consumer**: the number of slots consumed. The indices are the only shared atomics. The question is what makes the plain `buf_` accesses safe. ## Release and acquire, as C++ defines them The standard defines the pairing precisely. A store with `memory_order::release` (or `acq_rel`, `seq_cst`) performs a **release operation**; a load with `memory_order::acquire` performs an **acquire operation**. A release operation on an atomic object **synchronizes with** an acquire operation on the same object that takes its value from that store (or from later writes in its release sequence). Synchronizes-with is part of **happens-before**, and happens-before is what decides whether a plain access races. Applied to the ring: 1. Producer: write `buf_[h % N] = s;` — a plain store. 2. Producer: `head_.store(h + 1, std::memory_order_release);` 3. Consumer: `head_.load(std::memory_order_acquire)` returns `h + 1`. 4. Consumer: `out = buf_[h % N];` — a plain load. Step 2 synchronizes with step 3, so step 1 happens before step 4, and the plain read sees the sample. The `tail_` index runs the same handshake the other way: the consumer's release store of `t + 1` tells the producer, via its acquire load, that the slot's read is finished before the producer reuses it. ## Which loads can be relaxed Each thread loads **its own** index with `memory_order::relaxed`. Only that thread ever writes it, so it always reads its own last value, and that load publishes nothing. The two cross-thread loads — producer reading `tail_`, consumer reading `head_` — must be acquire. ## What breaks with relaxed everywhere | Operation | Correct order | If relaxed | |---|---|---| | producer `head_.store` | release | the sample write is not ordered before the index | | consumer `head_.load` | acquire | the slot read is not ordered after the index | | consumer `tail_.store` | release | the producer may overwrite a slot still being read | | producer `tail_.load` | acquire | same, from the producer's side | With relaxed indices the program still compiles and usually works on x86-64, whose hardware keeps stores in order; clang there even emits a plain `mov` for both a release store and a relaxed one. But the **compiler** may still reorder, weakly ordered CPUs such as arm64 really do reorder, and in the standard's terms the plain `buf_` accesses form a **data race** — undefined behavior. Building the relaxed variant with `-fsanitize=thread` produces `WARNING: ThreadSanitizer: data race` on the buffer read; the release/acquire version runs clean. ## Fences and other spellings The same ordering can be written with **fences**: a relaxed store preceded by `std::atomic_thread_fence(std::memory_order_release)` synchronises with a relaxed load followed by `std::atomic_thread_fence(std::memory_order_acquire)` when the load reads that store. Fences help when one fence covers several atomics, but they are easier to misplace. `memory_order::consume` was meant to be a cheaper acquire for dependent loads; mainstream compilers have long implemented it as acquire, and C++26 deprecates it and gives it acquire's meaning outright. ## Senior pitfalls - Placing `head_` and `tail_` on the same cache line makes the two cores fight over it; padding each to `std::hardware_destructive_interference_size` (C++17) is a common fix. - More than one producer breaks the design: `h` read relaxed is only correct while a single thread writes `head_`. - Index arithmetic: the indices only grow and the code compares the unsigned difference `h - t`, which stays correct across wrap-around; with `N` a power of two, `h % N` stays continuous there too.

  • Would seq_cst on every index operation also be correct here?
    Yes — seq_cst stores are release operations and seq_cst loads are acquire operations, so the handshake still holds, plus a single total order the ring does not need. The cost is target-dependent: clang on x86-64 compiles a seq_cst store to `xchg` instead of a plain `mov`, and on arm64 a seq_cst load uses `ldar` where acquire can use `ldapr`. Correct but slower on the hot path.
  • Why can't this ring take a second producer thread?
    The producer loads `head_` relaxed and then stores `head_ + 1`, assuming nobody else writes it. Two producers could read the same `h`, both write slot `h % N` (a data race on the plain buffer) and both publish `h + 1`, losing a sample. Multiple producers need a read-modify-write to claim slots — `fetch_add` or a `compare_exchange_weak` loop — plus per-slot publication, which is a different design.

A courier puts the parcel in a locker and only then texts the locker number; the recipient opens the locker only after reading the text. The text is the release store, reading it is the acquire load. Relaxed is a text that may arrive before the parcel is inside.

  • The indices are atomic, so the plain buffer cannot race.
  • Relaxed is fine because x86-64 never reorders stores.
  • The producer's acquire load of its own head index is required.
  • A release store alone makes the data visible, whatever the consumer's load order.
  • volatile on the buffer would remove the need for acquire and release.

Assumes C++23; the release/acquire rules date from C++11, and memory_order::consume is deprecated as of C++26.