atomic_fetch_add(), atomic_fetch_add_explicit()
Atomically add to the value of an atomic object (C11)
Synopsis:
#include <stdatomic.h>
C atomic_fetch_add( volatile A *obj,
M amount );
C atomic_fetch_add_explicit( volatile A *obj,
M amount,
memory_order order );
Arguments:
- obj
- A pointer to the atomic object (see the atomic_* types) whose value you want to add to.
- amount
- The value you want to add to the object.
M
is either the non-atomic type corresponding to A if A is an atomic integer type, or ptrdiff_t if A is an atomic pointer type. - order
- (atomic_fetch_add_explicit() only) The memory_order to use for the operation.
Library:
- If the atomic function is lock-free (see atomic_is_lock_free()), no extra library is required.
- If the function isn't lock-free, you need to link against libatomic. Use the -l atomic option to qcc to link against this library.
Description:
The atomic_fetch_add() and atomic_fetch_add_explicit() functions are generic functions that atomically add the given value to the given atomic object. The atomic_fetch_add() function orders memory access according to memory_order_seq_cst; atomic_fetch_add_explicit() orders them as specified by order.
The implementation of atomic functions may depend on the architecture.
For more information, see
LL/SC vs LSE atomic operations
in the description in Building Embedded Systems of the cpuinfo area of the system page.
Returns:
The previous value of the atomic object.
The C
represents the non-atomic data type that corresponds to the atomic object.
Classification:
Safety: | |
---|---|
Cancellation point | No |
Signal handler | Read the Caveats |
Thread | Yes |
Caveats:
If this function is lock-free (see atomic_is_lock_free()), it's safe to call it from a signal handler.