| Defined in header |
_Bool atomic_compare_exchange_strong( volatile A* obj, C* expected, C desired );
_Bool atomic_compare_exchange_weak( volatile A *obj, C* expected, C desired );
_Bool atomic_compare_exchange_strong_explicit( volatile A* obj, C* expected, C desired, memory_order succ, memory_order fail );
_Bool atomic_compare_exchange_weak_explicit( volatile A *obj, C* expected, C desired, memory_order succ, memory_order fail );
Atomically compares the value pointed to by
obj with the value pointed to by
expected, and if those are equal, replaces the former with
desired (performs read-modify-write operation). Otherwise, loads the actual value pointed to by
*expected (performs load operation).
The memory models for the read-modify-write and load operations are
fail respectively. The (1-2) versions use
memory_order_seq_cst by default.
The weak forms ((2) and (4)) of the functions are allowed to fail spuriously, that is, act as if
*obj != *expected even if they are equal. When a compare-and-exchange is in a loop, the weak version will yield better performance on some platforms. When a weak compare-and-exchange would require a loop and a strong one would not, the strong one is preferable.
This is a generic function defined for all atomic object types
A. The argument is pointer to a volatile atomic type to accept addresses of both non-volatile and volatile (e.g. memory-mapped I/O) atomic variables.
C is the non-atomic type corresponding to
|obj||-||pointer to the atomic object to test and modify|
|expected||-||pointer to the value expected to be found in the atomic object|
|desired||-||the value to store in the atomic object if it is as expected|
|succ||-||the memory synchronization ordering for the read-modify-write operation if the comparison succeeds. All values are permitted.|
|fail||-|| the memory synchronization ordering for the load operation if the comparison fails. Cannot be
The result of the comparison:
*obj was equal to
The behavior of
atomic_compare_exchange_strong is as if the following was executed atomically:
if (memcmp(obj, expected, sizeof *obj) == 0) memcpy(obj, &desired, sizeof *obj); else memcpy(expected, obj, sizeof *obj);
(the standard wording which says the comparison is in terms of "value" was changed to "bitwise equality" by DR 431).
| swaps a value with the value of an atomic object
| C++ documentation for
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.