Namespaces
Variants
Views
Actions

atomic_compare_exchange_weak, atomic_compare_exchange_strong, atomic_compare_exchange_weak_explicit, atomic_compare_exchange_strong_explicit

From cppreference.com
< c‎ | atomic
Revision as of 23:33, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <stdatomic.h>
</td>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
_Bool atomic_compare_exchange_strong( volatile A* obj,
                                      C* expected, C desired );
</td>

<td > (1) </td> <td > (since C11) </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
_Bool atomic_compare_exchange_weak( volatile A *obj,
                                    C* expected, C desired );
</td>

<td > (2) </td> <td > (since C11) </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
_Bool atomic_compare_exchange_strong_explicit( volatile A* obj,

                                               C* expected, C desired,
                                               memory_order succ,

                                               memory_order fail );
</td>

<td > (3) </td> <td > (since C11) </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
_Bool atomic_compare_exchange_weak_explicit( volatile A *obj,

                                             C* expected, C desired,
                                             memory_order succ,

                                             memory_order fail );
</td>

<td > (4) </td> <td > (since C11) </td> </tr> Template:ddcl list end

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 obj into *expected (performs load operation).

The memory models for the read-modify-write and load operations are succ and fail respectively. The (1-2) versions use memory_order_seq_cst by default.

The weak forms ((1) and (3)) 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 is the type of an atomic object, C is the non-atomic type corresponding to A.

Parameters

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 memory_order_release or memory_order_ack_rel and cannot specify stronger ordering than succ

Return value

The result of the comparison: true if *obj was equal to *exp, false otherwise.

See also

Template:c/atomic/dcl list atomic exchange
C++ documentation for atomic_compare_exchange_weak, atomic_compare_exchange_strong, atomic_compare_exchange_weak_explicit, atomic_compare_exchange_strong_explicit