Atomically compare the value to an expected value, and store a new value if the values are equal. If the values were not equal, update the expected value with the value read.
bool compare_exchange_strong( integral-type& expected, integral-type new_value, memory_order order = std::memory_order_seq_cst) volatile; bool compare_exchange_strong( integral-type& expected, integral-type new_value, memory_order success_order, memory_order failure_order) volatile;
- Preconditions:
failure_ordershall not bestd::memory_order_releaseorstd::memory_order_acq_rel.- Effects:
Atomically compares
expectedto the value stored in*thisand storesnew_valuein*thisif equal, otherwise updatesexpectedto the value read.- Returns:
trueif the existing value of*thiswas equal toexpected,falseotherwise.- Throws:
Nothing.
- Note:
The 3-parameter overload is equivalent to the 4-parameter overload with
success_order==order, andfailure_order==order, except that iforderisstd::memory_order_acq_rel, thenfailure_orderisstd::memory_order_acquire, and iforderisstd::memory_order_releasethenfailure_orderisstd::memory_order_relaxed.- Note:
This is an atomic read-modify-write operation for the memory location comprising
*thisif the result istrue, with memory orderingsuccess_order, otherwise it is an atomic load operation for the memory location comprising*thiswith memory orderingfailure_order.
#include <atomic>