<stdatomic.h> is the C11 header for atomic operations: reads and writes that other threads see as one indivisible step. It defines types such as atomic_int and operations such as atomic_fetch_add.
Atomics let threads share counters, flags, and pointers without a mutex. Each operation also takes a memory order that controls how surrounding reads and writes are ordered; the plain forms use sequentially consistent ordering, the safest default. Support is optional: an implementation without atomics defines __STDC_NO_ATOMICS__.
Remember
#include <stdatomic.h>
atomic_int counter = 0;
atomic_store(&counter, 5);
int v = atomic_load(&counter);
int old = atomic_fetch_add(&counter, 1); /* returns the old value */
atomic_compare_exchange_strong(&obj, &expected, desired);
atomic_flag f = ATOMIC_FLAG_INIT;
atomic_flag_test_and_set(&f);
Use
Prefer
Shared counter
atomic_int + atomic_fetch_add
Stop flag between threads
atomic_bool with atomic_load / atomic_store
Lock-free update
atomic_compare_exchange_strong
Simple spin lock
atomic_flag
Approach
How to Use stdatomic.h in C
Declare an atomic type, then use the named operations through its address.
Add #include <stdatomic.h> and compile with -std=c11.
Declare an atomic object such as atomic_int, atomic_bool, or _Atomic(T).
Use atomic_load, atomic_store, and atomic_fetch_* with the object's address.
Stay with the default sequentially consistent order until you have a reason to relax it.
Add -pthread when the atomics are shared by threads.
Piece
Required?
Role
#include <stdatomic.h>
Yes
Declares atomic types and functions
Atomic type
Yes
atomic_int or _Atomic(T)
-pthread
With threads
Needed on some systems when threads share the data
Pseudocode
Pseudocode
atomic_int counter = 0
old = atomic_fetch_add(&counter, 5) // old = 0, counter = 5
v = atomic_load(&counter) // 5
ok = atomic_compare_exchange_strong(&x, &expected, desired)
if x == expected: x = desired, ok = true
else: expected = x, ok = false
Reference
Cheat Sheet
Common names in stdatomic.h.
Name
Meaning
atomic_int, atomic_bool, atomic_size_t
Atomic versions of common types
_Atomic(T)
Atomic version of any suitable type T
atomic_load, atomic_store
Atomic read and write
atomic_fetch_add, _sub, _or, _and, _xor
Read-modify-write; return the old value
atomic_exchange
Store a new value and return the old one
atomic_compare_exchange_strong / _weak
Conditional swap
atomic_flag_test_and_set, atomic_flag_clear
Lock-free boolean flag
memory_order_relaxed … memory_order_seq_cst
Ordering constraints
atomic_thread_fence
Stand-alone memory fence
Trace
Worked Walkthrough — Fetch-Add and Compare-Exchange
Follow a counter through two updates and one conditional swap.
One line:stdatomic.h lets threads share simple values safely without taking a lock.
Frequently Asked Questions
stdatomic.h is the C11 header for atomic types and operations such as atomic_int, atomic_load, atomic_store, and atomic_fetch_add.
It adds to the atomic object and returns the value the object held before the addition.
It updates a value only if it still equals an expected value, which is the building block of lock-free algorithms. On failure the current value is copied into expected.
For one shared counter or flag, atomics are enough. For protecting several variables that must change together, use a mutex from threads.h.
It controls how reads and writes around an atomic operation can be reordered. The default, memory_order_seq_cst, is the strongest and safest.
It is optional in C11. Check that __STDC_NO_ATOMICS__ is not defined. GCC, Clang, and recent MSVC provide it.