atomic_compare_exchange_strong is a function that compares memory contents pointed to by two different objects.
bool atomic_compare_exchange_strong( volatile A* obj, C* expected, C desired );
The function compares the value of
expected, and if they are bitwise equal, it replaces the value contained in
desired value. Otherwise, it replaces the value pointed by
expected with the one contained in
obj by performing a load operation.
The operation of the function described above, where it reads and replaces data is atomic. This means that other threads cannot change the value between the time it is read and replaced.
As the name suggests, this function makes strong comparisons which means that the actual contents on the disk are compared. This comparison may fail for some values that compare using the
Unlike its counterpart,
atomic_compare_exchange_weak, this function only returns true when both the values pointed by
expected are indeed equal to each other. It does not allow any spurious failures. But, in some scenarios where loops are used,
atomic_compare_exchange_weak is a better option performance-wise.
objis a pointer to an atomic object to be compared.
expectedis a pointer to the value which is to be compared with the one contained in
desiredis the value to store in
objif the comparison is successful.
The function returns
true if the values pointed by
expected are equal and
false if otherwise.
View all Courses