Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What is atomic_compare_exchange_strong_explicit in C?

Adnan Abbas

atomic_compare_exchange_strong_explicit is a function that compares memory contents pointed to by two different objects. In addition, it also allows you to specify the memory order in the case of success and failure. memory_order is used as an argument to functions that involve atomic operations for synchronization of operations on other threads.


bool atomic_compare_exchange_strong_explicit( volatile A* obj, C* expected, C desired, memory_order success, memory_order failure );

The function compares the value of obj with expected. If they are bitwise equal, it replaces the value contained in obj with the 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 == operator.

Unlike its counterpart, atomic_compare_exchange_weak, this function only returns true when both the values pointed by obj and expected are indeed equal to each other. Hence, 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.


  • obj is a pointer to an atomic object to be compared.
  • expected is a pointer to the value that is to be compared with the one contained in obj.
  • desired is the value to store in obj if the comparison is successful.
  • success is the synchronization mode when the comparison is successful. All memory_order values are accepted in this argument.
  • failure is the synchronization mode when the comparison is unsuccessful. All memory_order values except memory_order_release, memory_order_acq_rel and the ones stronger than success are accepted in this argument.

Return value

The function returns true if the values pointed by obj and expected are equal and false if otherwise.




Adnan Abbas
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring