Namespaces
Variants
Views
Actions

std::atomic::compare_exchange_weak, std::atomic::compare_exchange_strong

From cppreference.com
< cpp‎ | atomic‎ | atomic
Revision as of 17:55, 2 November 2012 by P12bot (Talk | contribs)

 
 
 
 

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

<td>
Defined in header <atomic>
</td>

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

<tr class="t-dcl ">

<td >
bool compare_exchange_weak( T& expected, T desired,

                            std::memory_order success,
                            std::memory_order failure );

bool compare_exchange_weak( T& expected, T desired,
                            std::memory_order success,

                            std::memory_order failure ) volatile;
</td>

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

<tr class="t-dcl ">

<td >
bool compare_exchange_weak( T& expected, T desired,

                            std::memory_order order =
                                std::memory_order_seq_cst );

bool compare_exchange_weak( T& expected, T desired,
                            std::memory_order order =

                                std::memory_order_seq_cst ) volatile;
</td>

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

<tr class="t-dcl ">

<td >
bool compare_exchange_strong( T& expected, T desired,

                              std::memory_order success,
                              std::memory_order failure );

bool compare_exchange_strong( T& expected, T desired,
                              std::memory_order success,

                              std::memory_order failure ) volatile;
</td>

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

<tr class="t-dcl ">

<td >
bool compare_exchange_strong( T& expected, T desired,

                              std::memory_order order =
                                  std::memory_order_seq_cst );

bool compare_exchange_strong( T& expected, T desired,
                              std::memory_order order =

                                  std::memory_order_seq_cst ) volatile;
</td>

<td > (4) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Atomically compares the value stored in *this 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 stored in *this into *expected (performs load operation).

The memory models for the read-modify-write and load operations are success and failure respectively. In the (2) and (4) versions order is used for both read-modify-write and load operations, except that std::memory_order_release and std::memory_order_relaxed are used for the load operation if order == std::memory_order_acq_rel, or order == std::memory_order_release respectively.

The weak forms (1-2) of the functions are allowed to fail spuriously, that is, act as if *this != *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.

Contents

Parameters

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
success - the memory synchronization ordering for the read-modify-write operation if the comparison succeeds. All values are permitted.
failure - the memory synchronization ordering for the load operation if the comparison fails. Cannot be std::memory_order_release or std::memory_order_ack_rel and cannot specify stronger ordering than success
order - the memory synchronization ordering for both operations

Return value

true if the underlying atomic value was changed, false otherwise.

Exceptions

noexcept specification:  
noexcept
  

Example

Demonstrates how compare_exchange_strong either changes the value of the atomic variable or the variable used for comparison.

#include <atomic>
#include <iostream>
 
std::atomic<int>  ai;
 
int  tst_val= 4;
int  new_val= 5;
bool exchanged= false;
 
void valsout()
{
    std::cout << "ai= " << ai
	      << "  tst_val= " << tst_val
	      << "  new_val= " << new_val
	      << "  exchanged= " << std::boolalpha << exchanged
	      << "\n";
}
 
int main()
{
    ai= 3;
    valsout();
 
    // tst_val != ai   ==>  tst_val is modified
    exchanged= ai.compare_exchange_strong( tst_val, new_val );
    valsout();
 
    // tst_val == ai   ==>  ai is modified
    exchanged= ai.compare_exchange_strong( tst_val, new_val );
    valsout();
 
    return 0;
}

Output:

ai= 3  tst_val= 4  new_val= 5  exchanged= false
ai= 3  tst_val= 3  new_val= 5  exchanged= false
ai= 5  tst_val= 3  new_val= 5  exchanged= true

See also

Template:cpp/atomic/dcl list atomic compare exchange