The Open Group Base Specifications Issue 8
IEEE Std 1003.1-2024
Copyright © 2001-2024 The IEEE and The Open Group

NAME

atomic_fetch_add, atomic_fetch_add_explicit, atomic_fetch_and, atomic_fetch_and_explicit, atomic_fetch_or, atomic_fetch_or_explicit, atomic_fetch_sub, atomic_fetch_sub_explicit, atomic_fetch_xor, atomic_fetch_xor_explicit — atomically replace the value of an object with the result of a computation

SYNOPSIS

#include <stdatomic.h>

C atomic_fetch_add(volatile A *object, M operand);
C atomic_fetch_add_explicit(volatile A *object, M operand,
       memory_order
order);
C atomic_fetch_and(volatile A *object, M operand);
C atomic_fetch_and_explicit(volatile A *object, M operand,
       memory_order
order);
C atomic_fetch_or(volatile A *object, M operand);
C atomic_fetch_or_explicit(volatile A *object, M operand,
       memory_order
order);
C atomic_fetch_sub(volatile A *object, M operand);
C atomic_fetch_sub_explicit(volatile A *object, M operand,
       memory_order
order);
C atomic_fetch_xor(volatile A *object, M operand);
C atomic_fetch_xor_explicit(volatile A *object, M operand,
       memory_order
order);

DESCRIPTION

[CX] [Option Start] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2024 defers to the ISO C standard. [Option End]

Implementations that define the macro __STDC_NO_ATOMICS__ need not provide the <stdatomic.h> header nor support these generic functions.

The atomic_fetch_add_explicit() generic function shall atomically replace the value pointed to by object with the result of adding operand to this value. This operation shall be an atomic read-modify-write operation (see XBD 4.15.1 Memory Ordering ). Memory shall be affected according to the value of order.

The atomic_fetch_add() generic function shall be equivalent to atomic_fetch_add_explicit() called with order set to memory_order_seq_cst.

The other atomic_fetch_*() generic functions shall be equivalent to atomic_fetch_add_explicit() if their name ends with explicit, or to atomic_fetch_add() if it does not, respectively, except that they perform the computation indicated in their name, instead of addition:

sub
subtraction
or
bitwise inclusive OR
xor
bitwise exclusive OR
and
bitwise AND

For addition and subtraction, the application shall ensure that A is an atomic integer type or an atomic pointer type and is not atomic_bool. For the other operations, the application shall ensure that A is an atomic integer type and is not atomic_bool.

For signed integer types, the computation shall silently wrap around on overflow; there are no undefined results. For pointer types, the result can be an undefined address, but the computations otherwise have no undefined behavior.

RETURN VALUE

These generic functions shall return the value pointed to by object immediately before the effects.

ERRORS

No errors are defined.


The following sections are informative.

EXAMPLES

None.

APPLICATION USAGE

The operation of these generic functions is nearly equivalent to the operation of the corresponding compound assignment operators +=, -=, etc. The only differences are that the compound assignment operators are not guaranteed to operate atomically, and the value yielded by a compound assignment operator is the updated value of the object, whereas the value returned by these generic functions is the previous value of the atomic object.

RATIONALE

None.

FUTURE DIRECTIONS

None.

SEE ALSO

XBD 4.15.1 Memory Ordering , <stdatomic.h>

CHANGE HISTORY

First released in Issue 8. Included for alignment with the ISO/IEC 9899:2018 standard.

End of informative text.

 

return to top of page

UNIX® is a registered Trademark of The Open Group.
POSIX™ is a Trademark of The IEEE.
Copyright © 2001-2024 The IEEE and The Open Group, All Rights Reserved
[ Main Index | XBD | XSH | XCU | XRAT ]