stdatomic(3) Library Functions Manual stdatomic(3)

ATOMIC_VAR_INIT, atomic_init, atomic_load, atomic_store, atomic_exchange, atomic_compare_exchange_strong, atomic_compare_exchange_weak, atomic_fetch_add, atomic_fetch_and, atomic_fetch_or, atomic_fetch_sub, atomic_fetch_xor, atomic_is_lock_freetype-generic atomic operations

#include <stdatomic.h>


_Atomic(T) v = ATOMIC_VAR_INIT(c);
_Atomic T v = ATOMIC_VAR_INIT(c);

void
atomic_init(_Atomic(T) *object, T value);

T
atomic_load(_Atomic(T) *object);

T
atomic_load_explicit(_Atomic(T) *object, memory_order order);

void
atomic_store(_Atomic(T) *object, T desired);

void
atomic_store_explicit(_Atomic(T) *object, T desired, memory_order order);

T
atomic_exchange(_Atomic(T) *object, T desired);

T
atomic_exchange_explicit(_Atomic(T) *object, T desired, memory_order order);

_Bool
atomic_compare_exchange_strong(_Atomic(T) *object, T *expected, T desired);

_Bool
atomic_compare_exchange_strong_explicit(_Atomic(T) *object, T *expected, T desired, memory_order success, memory_order failure);

_Bool
atomic_compare_exchange_weak(_Atomic(T) *object, T *expected, T desired);

_Bool
atomic_compare_exchange_weak_explicit(_Atomic(T) *object, T *expected, T desired, memory_order success, memory_order failure);

T
atomic_fetch_add(_Atomic(T) *object, T operand);

T
atomic_fetch_add_explicit(_Atomic(T) *object, T operand, memory_order order);

T
atomic_fetch_and(_Atomic(T) *object, T operand);

T
atomic_fetch_and_explicit(_Atomic(T) *object, T operand, memory_order order);

T
atomic_fetch_or(_Atomic(T) *object, T operand);

T
atomic_fetch_or_explicit(_Atomic(T) *object, T operand, memory_order order);

T
atomic_fetch_sub(_Atomic(T) *object, T operand);

T
atomic_fetch_sub_explicit(_Atomic(T) *object, T operand, memory_order order);

T
atomic_fetch_xor(_Atomic(T) *object, T operand);

T
atomic_fetch_xor_explicit(_Atomic(T) *object, T operand, memory_order order);

_Bool
atomic_is_lock_free(const _Atomic(T) *object);

The header <stdatomic.h> provides type-generic operations on atomic operations.

Atomic variables are declared using the _Atomic() type specifier or the _Atomic type qualifier. Such variables are not type-compatible with their non-atomic counterparts and may have different alignment.

Operations on atomic variables that do not use the () interfaces, including compound assignment operations, will behave as if the non-_explicit() versions of those interfaces had been used.

The () operation initializes the atomic variable object with value. Atomic variables can be initialized while being declared using ().

The () operation returns the value of atomic variable object. The () operation sets the atomic variable object to the desired value.

The () operation combines the behaviour of atomic_load() and atomic_store(). It sets the atomic variable object to the desired value and returns the original contents of the atomic variable.

The () operation stores the desired value into atomic variable object, but only if the atomic variable is equal to the expected value. Upon success, the operation returns true. Upon failure, the expected value is overwritten with the contents of the atomic variable and false is returned.

The () operation is identical to atomic_compare_exchange_strong(), but is allowed to fail even if atomic variable object is equal to the expected value. When an () operation is in a loop, the weak version will yield better performance on some platforms. When atomic_compare_exchange_weak() would require a loop and atomic_compare_exchange_strong() would not, the strong version is preferable.

The () operation adds the value operand to atomic variable object and returns the original contents of the atomic variable.

The () operation applies the operator to atomic variable object and value operand and stores the result into object, while returning the original contents of the atomic variable.

The () operation applies the operator to atomic variable object and value operand and stores the result into object, while returning the original contents of the atomic variable.

The () operation subtracts the value operand from atomic variable object and returns the original contents of the atomic variable.

The () operation applies the operator to atomic variable object and value operand and stores the result into object, while returning the original contents of the atomic variable.

The () operation returns whether atomic variable object uses locks to implement atomic operations.

C11 defines a memory model that may allow for the reordering of operations in the absence of fences or explicit memory ordering operations. The non-_explicit() interfaces use the strictest available memory order: sequential consistency. The _explicit() interfaces allow for configuration of the memory order operation which is present. The types of available memory order operations are explained in more detail in ISO/IEC 9899:2011 (“ISO C11”).

The order parameter of the () interfaces can have one of the following values:

Operation does not order memory.
Performs a consume operation.
Performs an acquire operation.
Performs a release operation.
Performs both an acquire and a release operation.
Provides sequential consistency.

atomic(3), pthread(3)

These interfaces conform to ISO/IEC 9899:2011 (“ISO C11”).

December 27, 2011 macOS 14.6