Memory Order
Memory order is the constraint you attach to an atomic operation that says how it may be reordered with other accesses. It is the C11 model for what "this write becomes visible before that read" means.
Detailed Explanation
Each thread runs as if its own ordinary accesses happen in program order, from its point of view. Other threads may see those accesses in a different order, and the compiler may move them, unless something synchronizes. Mutex unlock / lock is one such something. Atomic operations with the right orders are another.
memory_order_relaxed is atomicity only: no tear, no race, no ordering. Counters that nobody uses to publish other data can be relaxed.
memory_order_release on a store makes prior writes in this thread visible to a thread that does an acquire load of the same atomic and sees the value. The flag publish pattern is: write the payload with ordinary stores, then atomic_store_explicit(&ready, 1, memory_order_release). The reader does atomic_load_explicit(&ready, memory_order_acquire) and, if it sees 1, may read the payload. That pair is the replacement for a broken volatile int ready.
memory_order_acq_rel is both, for read-modify-write. memory_order_seq_cst is acquire/release plus a single global order of all seq-cst operations. It is the default and the one you can reason about without a graph.
memory_order_consume exists on paper and is treated as acquire by compilers. Do not invent a consume design.
You do not put a memory order on a mutex; the mutex operations already synchronize. You do not need a fence between two seq-cst operations on the same variable. atomic_thread_fence is for cases where the atomic operation itself is relaxed and you want a barrier nearby; it is a specialist tool.
If this page feels slippery, use mutexes or seq-cst atomics. Wrong orders look like "a thread missed a write that happened" and reproduce on one CPU family.
Code Examples
#include <stdatomic.h>
#include <pthread.h>
#include <stdio.h>
static int payload;
static _Atomic int ready;
static void *producer(void *arg)
{
(void)arg;
payload = 42; /* ordinary write */
atomic_store_explicit(&ready, 1, memory_order_release);
return NULL;
}
static void *consumer(void *arg)
{
(void)arg;
while (atomic_load_explicit(&ready, memory_order_acquire) == 0) {
/* spin */
}
printf("%d\n", payload); /* prints: 42 */
return NULL;
}
int main(void)
{
pthread_t a, b;
pthread_create(&a, NULL, producer, NULL);
pthread_create(&b, NULL, consumer, NULL);
pthread_join(a, NULL);
pthread_join(b, NULL);
return 0;
}Common Gotchas
Publishing a buffer with a `relaxed` store of `ready` lets the reader see `ready == 1` and stale payload
use release/acquire or seq-cst.
`seq_cst` is not a reason to skip a mutex on a multi-field invariant
order is not the same as "both fields together."
x86 is a strong platform
a buggy `relaxed` design may pass there and fail on ARM. Test weak hardware or use a model checker.
Spinning on acquire without a pause burns a core
for a long wait, use a condvar.