Atomic Operations
Mutexes work, but they aren't free. Once locks are contended, you're paying for syscalls and possibly context switches. (Uncontended, modern mutex implementations like Linux futexes stay in userspace as plain atomic ops, but the moment two threads collide the cost shoots up.) For simple jobs — bump a counter, flip a flag — atomic operations give you synchronisation with no lock at all.
C11 brought us <stdatomic.h>, which exposes portable atomic types and memory-ordering controls.
Atomic Types and Basic Operations
#include <stdatomic.h>#include <stdio.h>#include <pthread.h>
atomic_int counter = 0;
void *increment(void *arg) { for (int i = 0; i < 100000; i++) { atomic_fetch_add(&counter, 1); } return NULL;}
int main() { pthread_t t1, t2; pthread_create(&t1, NULL, increment, NULL); pthread_create(&t2, NULL, increment, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); printf("%d\n", atomic_load(&counter)); // Always 200000 return 0;}atomic_fetch_add does a read-modify-write as a single, indivisible operation. No mutex required, no data race.
Common atomic operations:
| Operation | Description |
|---|---|
atomic_load | Read the current value |
atomic_store | Write a new value |
atomic_fetch_add | Add and return old value |
atomic_fetch_sub | Subtract and return old value |
atomic_exchange | Swap and return old value |
atomic_compare_exchange_strong | CAS (compare-and-swap) |
Memory Ordering
The real power of atomics — and the headache — is memory ordering. Modern CPUs reorder instructions for performance. Memory-ordering constraints tell the CPU and compiler which reorderings are off-limits.
C11 defines several memory orders:
atomic_int flag = 0;
// Relaxed: no ordering guarantees beyond atomicityatomic_store_explicit(&flag, 1, memory_order_relaxed);
// Release: all writes before this are visible to a thread that acquiresatomic_store_explicit(&flag, 1, memory_order_release);
// Acquire: all writes released by the storing thread are visibleint val = atomic_load_explicit(&flag, memory_order_acquire);
// Sequentially consistent: total order across all threads (default)atomic_store(&flag, 1); // implicitly memory_order_seq_cstAcquire-release is the workhorse pattern. A thread storing with release paired with another loading with acquire forms a synchronizes-with relationship: every write the storing thread did before its release is guaranteed visible to the loading thread after its acquire.
The Acquire-Release Pattern
The classic example: a producer-consumer handoff.
#include <stdatomic.h>#include <pthread.h>#include <stdio.h>
int data = 0; // Non-atomic, protected by orderingatomic_int ready = 0;
void *producer(void *arg) { data = 42; // Write data atomic_store_explicit(&ready, 1, memory_order_release); // Signal return NULL;}
void *consumer(void *arg) { while (atomic_load_explicit(&ready, memory_order_acquire) == 0) { // spin } printf("Data: %d\n", data); // Guaranteed to see 42 return NULL;}The release store on ready pins the write to data to happen-before the consumer's read. Skip the memory ordering and the consumer can see ready == 1 paired with data == 0 — the CPU is perfectly happy to reorder those.
Compare-and-Swap (CAS)
Compare-and-swap is the foundation of every lock-free algorithm. Atomically: check if a variable equals an expected value, and if so, replace it with a new one.
atomic_int value = 0;
// Try to change value from 0 to 1int expected = 0;if (atomic_compare_exchange_strong(&value, &expected, 1)) { printf("Swapped successfully\n");} else { printf("Value was %d, not 0\n", expected);}When the swap fails, expected is rewritten with whatever the current value is, so you can loop and retry:
void atomic_multiply(atomic_int *val, int multiplier) { int expected = atomic_load(val); while (!atomic_compare_exchange_weak(val, &expected, expected * multiplier)) { // expected was updated with current value, retry }}compare_exchange_weak is allowed to fail spuriously (report failure even when the value did match) but rewards you with cheaper code on some architectures inside a retry loop.
The runner can't start threads, but CAS behaves the same with only one, which makes it a good place to pin down exactly what happens to expected. Predict all four lines before you run it.
The failed CAS is the line to stare at. It didn't touch value, but it did overwrite expected with the 1 it found, so a retry loop always works from fresh data.
A Lock-Free Stack
A classic lock-free stack built on CAS:
#include <stdatomic.h>#include <stdlib.h>#include <stdio.h>
typedef struct node { int data; struct node *next;} node;
typedef struct { _Atomic(node *) head;} lf_stack;
void lf_push(lf_stack *s, int value) { node *new_node = malloc(sizeof(node)); new_node->data = value; new_node->next = atomic_load(&s->head); while (!atomic_compare_exchange_weak(&s->head, &new_node->next, new_node)) { // CAS failed; new_node->next updated to current head, retry }}
int lf_pop(lf_stack *s, int *out) { node *old_head = atomic_load(&s->head); while (old_head != NULL) { if (atomic_compare_exchange_weak(&s->head, &old_head, old_head->next)) { *out = old_head->data; free(old_head); return 1; } } return 0; // empty}Multiple threads push and pop concurrently with no locks anywhere. When another thread races ahead and changes the head, the CAS loop just retries.
Don't ship this one, though. lf_pop has a memory-reclamation bug on top of the ABA problem described below: thread A loads old_head and is about to read old_head->next when thread B pops the same node and frees it, so A reads freed memory. Production lock-free structures solve this with hazard pointers, epoch-based reclamation or garbage collection, which is a big part of why they're hard.
When to Use Atomics vs Mutexes
Reach for atomics when:
- The operation is simple — counters, flags, single-pointer swaps
- Performance matters and you expect contention
- You're building lock-free data structures
Reach for mutexes when:
- The critical section spans multiple variables or non-trivial logic
- You need condition variables for waiting
- Correctness matters more than the last few percent of throughput
Lock-free programming is famously hard. Subtleties like the ABA problem — a value flips from A to B and back to A, fooling your CAS into thinking nothing happened — can cause real corruption. Unless your profiler is screaming "mutex contention," reach for the mutex first. Clarity wins.
Here's ABA hitting lf_pop on a stack holding A, B, C. Thread 1 is paused between reading old_head->next and its CAS. Meanwhile thread 2 pops A and B, then pushes a new node that malloc happens to place at A's freed address. The head is "A" again, so thread 1's CAS succeeds and installs B, a node that's already gone.
The usual cures pair the pointer with a counter that changes on every update (a tagged pointer, swapped with a double-width CAS) or make sure a node's memory can't be reused while anyone might still hold it.
Memory Order Summary
| Order | Guarantee |
|---|---|
relaxed | Atomicity only. No ordering. |
acquire | Reads after this see writes before a release. |
release | Writes before this are visible after an acquire. |
acq_rel | Both acquire and release (for read-modify-write). |
seq_cst | Total global order. Strongest, default. |
For most code, stay with the default memory_order_seq_cst — it's what the non-_explicit functions use. Only step down to weaker orders when profiling proves the synchronisation is hurting you and you actually understand the consequences.