Defined in header <atomic>  

(1)  (since C++11)  
template< class T > bool atomic_compare_exchange_weak( std::atomic<T>* obj, typename std::atomic<T>::value_type* expected, typename std::atomic<T>::value_type desired ) noexcept;  
template< class T > bool atomic_compare_exchange_weak( volatile std::atomic<T>* obj, typename std::atomic<T>::value_type* expected, typename std::atomic<T>::value_type desired ) noexcept;  
(2)  (since C++11)  
template< class T > bool atomic_compare_exchange_strong( std::atomic<T>* obj, typename std::atomic<T>::value_type* expected, typename std::atomic<T>::value_type desired ) noexcept;  
template< class T > bool atomic_compare_exchange_strong( volatile std::atomic<T>* obj, typename std::atomic<T>::value_type* expected, typename std::atomic<T>::value_type desired ) noexcept;  
(3)  (since C++11)  
template< class T > bool atomic_compare_exchange_weak_explicit( std::atomic<T>* obj, typename std::atomic<T>::value_type* expected, typename std::atomic<T>::value_type desired, std::memory_order succ, std::memory_order fail ) noexcept;  
template< class T > bool atomic_compare_exchange_weak_explicit( volatile std::atomic<T>* obj, typename std::atomic<T>::value_type* expected, typename std::atomic<T>::value_type desired, std::memory_order succ, std::memory_order fail ) noexcept;  
(4)  (since C++11)  
template< class T > bool atomic_compare_exchange_strong_explicit( std::atomic<T>* obj, typename std::atomic<T>::value_type* expected, typename std::atomic<T>::value_type desired, std::memory_order succ, std::memory_order fail ) noexcept;  
template< class T > bool atomic_compare_exchange_strong_explicit( volatile std::atomic<T>* obj, typename std::atomic<T>::value_type* expected, typename std::atomic<T>::value_type desired, std::memory_order succ, std::memory_order fail ) noexcept; 
Atomically compares the object representation (until C++20)value representation (since C++20) of the object pointed to by obj
with that of the object pointed to by expected
, and if those are bitwiseequal, replaces the former with desired
(performs readmodifywrite 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 readmodifywrite and load operations are succ
and fail
respectively. The (12) versions use std::memory_order_seq_cst
by default.
These functions are defined in terms of member functions of std::atomic
:
obj>compare_exchange_weak(*expected, desired)
obj>compare_exchange_strong(*expected, desired)
obj>compare_exchange_weak(*expected, desired, succ, fail)
obj>compare_exchange_strong(*expected, desired, succ, fail)
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 readmodifywrite 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) 
The result of the comparison: true
if *obj
was equal to *expected
, false
otherwise.
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 compareandexchange is in a loop, the weak version will yield better performance on some platforms.
When a weak compareandexchange 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, (until C++20) trap bits, or offers multiple object representations for the same value (e.g. floatingpoint NaN). In those cases, weak compareandexchange typically works because it quickly converges on some stable object representation.
For a union with bits that participate in the value representations of some members but not the others, compareandexchange might always fail because such padding bits have indeterminate values when they do not participate in the value representation of the active member.
Padding bits that never participate in an object's value representation are ignored.  (since C++20) 
compare and exchange operations are often used as basic building blocks of lockfree data structures.
#include <atomic> template<class T> struct node { T data; node* next; node(const T& data) : data(data), next(nullptr) {} }; template<class T> class stack { std::atomic<node<T>*> head; public: void push(const T& data) { node<T>* new_node = new node<T>(data); // put the current value of head into new_node>next new_node>next = head.load(std::memory_order_relaxed); // now make new_node the new head, but if the head // is no longer what's stored in new_node>next // (some other thread must have inserted a node just now) // then put that new head into new_node>next and try again while(!std::atomic_compare_exchange_weak_explicit( &head, &new_node>next, new_node, std::memory_order_release, std::memory_order_relaxed)) ; // the body of the loop is empty // note: the above loop is not threadsafe in at least // GCC prior to 4.8.3 (bug 60272), clang prior to 20140505 (bug 18899) // MSVC prior to 20140317 (bug 819819). See member function version for workaround } }; int main() { stack<int> s; s.push(1); s.push(2); s.push(3); }
The following behaviorchanging defect reports were applied retroactively to previously published C++ standards.
DR  Applied to  Behavior as published  Correct behavior 

P0558R1  C++11  exact type match required because T is deduced from multiple arguments  T is deduced from the atomic argument only 
atomically compares the value of the atomic object with nonatomic argument and performs atomic exchange if equal or atomic load if not (public member function of std::atomic<T> ) 

(C++11)(C++11)  atomically replaces the value of the atomic object with nonatomic argument and returns the old value of the atomic (function template) 
(deprecated in C++20)  specializes atomic operations for std::shared_ptr (function template) 
C documentation for atomic_compare_exchange, atomic_compare_exchange_explicit 
© cppreference.com
Licensed under the Creative Commons AttributionShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/atomic/atomic_compare_exchange