std::atomic_compare_exchange_weak, std::atomic_compare_exchange_strong, std::atomic_compare_exchange_weak_explicit, std::atomic_compare_exchange_strong_explicit

From cppreference.com
< cpp‎ | atomic
Defined in header <atomic>
(1) (since C++11)
template< class T >

bool atomic_compare_exchange_weak( std::atomic<T>* obj,

                                   T* expected, T desired );
template< class T >

bool atomic_compare_exchange_weak( volatile std::atomic<T>* obj,

                                   T* expected, T desired );
(2) (since C++11)
template< class T >

bool atomic_compare_exchange_strong( std::atomic<T>* obj,

                                     T* expected, T desired );
template< class T >

bool atomic_compare_exchange_strong( volatile std::atomic<T>* obj,

                                     T* expected, T desired );
(3) (since C++11)
template< class T >

bool atomic_compare_exchange_weak_explicit( std::atomic<T>* obj,
                                            T* expected, T desired,
                                            std::memory_order succ,

                                            std::memory_order fail );
template< class T >

bool atomic_compare_exchange_weak_explicit( volatile std::atomic<T>* obj,
                                            T* expected, T desired,
                                            std::memory_order succ,

                                            std::memory_order fail );
(4) (since C++11)
template< class T >

bool atomic_compare_exchange_strong_explicit( std::atomic<T>* obj,
                                              T* expected, T desired,
                                              std::memory_order succ,

                                              std::memory_order fail );
template< class T >

bool atomic_compare_exchange_strong_explicit( volatile std::atomic<T>* obj,
                                              T* expected, T desired,
                                              std::memory_order succ,

                                              std::memory_order fail );

Atomically compares the object representation of the object pointed to by obj with the object representation of the object pointed to by expected, as if by std::memcmp, and if those are bitwise-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). Copying is performed as if by std::memcpy.

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

These functions are defined in terms of member functions of std::atomic:

1) obj->compare_exchange_weak(*expected, desired)
2) obj->compare_exchange_strong(*expected, desired)
3) obj->compare_exchange_weak(*expected, desired, succ, fail)
4) obj->compare_exchange_strong(*expected, desired, succ, fail)

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 std::memory_order_release or std::memory_order_acq_rel and cannot specify stronger ordering than succ (until C++17)

Return value

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

Exceptions

noexcept specification:  
noexcept
  

Notes

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 unless the object representation of T may include padding bits, trap bits, or offers multiple object representations for the same value (e.g. floating-point NaN). In those cases, weak compare-and-exchange typically works because it quickly converges on some stable object representation.