The C11 feature most worth getting excited about is _Atomic together with the <stdatomic.h> header. It gives C a standard, portable way to share variables between threads without data races, and without a mutex for simple cases such as counters, flags and reference counts.
Before C11, portable lock-free code in C did not exist: you used compiler builtins such as GCC's __sync_* functions or platform APIs. C11 also standardised threads (<threads.h>) and a memory model, so atomics now have well-defined meaning in the language itself. C17 and C23 keep all of this.
_Atomic and <stdatomic.h> give you<threads.h>_Atomic details worth knowing_Atomic and <stdatomic.h> give youA plain int shared between threads and modified by more than one of them is a data race, which is undefined behaviour in C11. An atomic object avoids that: every read, write and read-modify-write on it is indivisible, and other threads see a consistent value.
There are two ways to declare one:
_Atomic int counter;<stdatomic.h>: atomic_int, atomic_bool, atomic_size_t, atomic_uintptr_t and so on. atomic_int is the same type as _Atomic int.The header also provides the operations: atomic_load, atomic_store, atomic_exchange, atomic_fetch_add, atomic_fetch_sub, atomic_fetch_or, atomic_fetch_and, atomic_compare_exchange_strong and atomic_compare_exchange_weak. Plain operators such as counter++ also work on atomic objects and are sequentially consistent, but the named functions make the intent and the memory order explicit.
<threads.h>This program starts four threads that each add 100,000 to a shared counter. It compiles with gcc -std=c11 -Wall -Wextra counter.c -pthread on a platform whose C library provides <threads.h> (glibc 2.28 and later, musl, and others).
atomic_fetch_add_explicit with memory_order_relaxed is enough here because the counter is only read after every thread has been joined, and thrd_join already synchronises. If you replace the atomic with a plain int, the result is usually lower than 400000 and changes from run to run.
<threads.h> is an optional part of C11: implementations that do not support it define __STDC_NO_THREADS__. Notably, Apple's libc does not ship it, so on macOS you use POSIX threads (pthread_create) with the same <stdatomic.h> code. Likewise __STDC_NO_ATOMICS__ signals a compiler without atomics.
Every atomic function has an _explicit variant that takes a memory_order. The plain versions use memory_order_seq_cst, the strongest and the safest default.
| Memory order | Guarantee | Typical use |
|---|---|---|
memory_order_relaxed | Atomicity only, no ordering with other memory | Statistics counters |
memory_order_acquire | Later reads and writes cannot move before this load | Reading a "ready" flag |
memory_order_release | Earlier reads and writes cannot move after this store | Publishing data, then setting the flag |
memory_order_acq_rel | Both, for read-modify-write operations | Lock-free stack push and pop |
memory_order_seq_cst | One global order seen by all threads | Default; use when unsure |
A release store paired with an acquire load is the classic publish pattern: the producer writes its data, then does a release store to a flag; the consumer does an acquire load of the flag and may then read the data safely.
Start with the default seq_cst. Move to weaker orders only when profiling shows it matters and you can explain why the weaker order is correct. memory_order_consume exists but is discouraged because compilers implement it as acquire.
When an update is not a single built-in operation, use atomic_compare_exchange_weak in a loop. This example records the maximum value seen by any thread.
The weak variant may fail spuriously on some architectures, which is why it belongs in a loop and is often faster than strong there. When the compare fails, C writes the value it actually found back into current, so the loop retries with fresh data.
_Atomic details worth knowingatomic_flag is the only type guaranteed to be lock-free. It supports atomic_flag_test_and_set and atomic_flag_clear and is the building block for a spinlock.atomic_is_lock_free(&obj) tells you whether an object uses hardware atomics. Large types, such as an _Atomic struct, may fall back to a hidden lock._Atomic(struct node *) head;), which is how lock-free stacks and queues are built. Beware the ABA problem when you pop and push nodes._Atomic(T) as a type specifier and _Atomic T as a qualifier are different spellings; a member of an atomic struct cannot be accessed atomically on its own.mtx_t or pthread_mutex_t).Atomics are not the only useful addition. These are also widely used:
_Static_assert (spelled static_assert with <assert.h>) checks conditions at compile time._Generic selects an expression by type, the basis for type-generic macros._Alignas and _Alignof control alignment, and aligned_alloc allocates aligned memory._Noreturn marks functions that never return.gets() was removed from the standard library; use fgets().volatile a substitute for _Atomic?No. volatile prevents the compiler from optimising accesses away but gives no atomicity or inter-thread ordering. Use _Atomic for shared data between threads.
<threads.h> to use atomics?No. <stdatomic.h> works with POSIX threads, Windows threads or any other threading library, as long as your compiler supports C11 atomics.
Not always. An uncontended atomic operation is cheap, but heavily contended atomics can still stall cores. A mutex is simpler and often fast enough when you protect more than one variable.
GCC (from 4.9), Clang, and MSVC (from Visual Studio 2022 17.5 with /std:c11 and /experimental:c11atomics or later releases) support them. Check your compiler documentation for the exact flags.
_Atomic and <stdatomic.h> make multi-threaded C safer and more portable: use atomic_int or _Atomic for shared counters and flags, atomic_fetch_* for simple updates, and compare-and-swap loops for custom lock-free logic. Default to sequentially consistent ordering, use acquire and release for publishing data, and reach for a mutex when state spans several variables.