mercredi 10 mars 2021

Why does compare_exchange_strong fail with std::atomic

I am reading C++ Concurrency in Action Chapter 5. This chapter says,

Note that although you can use std::atomic<float> or std::atomic<double>, because the built-in floating point types do satisfy the criteria for use with memcpy and memcmp, the behavior may be surprising in the case of compare_exchange_strong. The operation may fail even though the old stored value was equal in value to the comparand, if the stored value had a different representation. Note that there are no atomic arithmetic operations on floating-point values. You’ll get similar behavior with compare_exchange_strong if you use std::atomic<> with a user-defined type that has an equality-comparison operator defined, and that operator differs from the comparison using memcmp—the operation may fail because the otherwise-equal values have a different representation.

But I don't understand why it is.

  1. If float and double can use memcpy and memcmp, what is the matter to do atomic operation like compare_exchange_strong ?

  2. I can't use compare_exchange_weak also ?

  3. Above paragraph, what does "difference representation" mean ?

Aucun commentaire:

Enregistrer un commentaire