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. Additionally, this may fail spuriously (and
return false) even if the
values were equal, if the value could not be updated atomically.
bool compare_exchange_weak( BaseType& expected,BaseType new_value, memory_order order = std::memory_order_seq_cst) volatile; bool compare_exchange_weak( BaseType& expected,BaseType 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 toexpectedandnew_valuewas successfully stored in*this,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.
Header
#include <atomic>