atomic_* functions.
Memory ordering
All atomic operations accept amemory_order argument:
The default is
"relaxed" unless otherwise specified.
Atomic load and store
Atomic exchange
Returns the old value and storesvalue atomically:
Atomic fetch-and-modify (RMW)
Each operation atomically reads the current value, applies the operation, stores the result, and returns the old value.Example: global counter
Example: parallel histogram
Compare-and-swap (CAS)
atomic_compare_exchange_weak attempts to replace expected with desired. Returns True if the swap succeeded:
Example: lock-free maximum
Ordering guidelines
Using
"relaxed" where ordering matters is a common source of subtle bugs. When in doubt, use "acq_rel".