Even tough you can create atomic<float>
and atomic<double>
, atomic operators are not defined for floating point atomics. This is because there is no x86 (nor ARM) assembly instruction for atomically add floating point values.
A workaround is to use the compare_exchange operations to increment/change your atomic variable.
#include <atomic>
int main()
{
std::atomic<int> i{};
i += 3;
// Peter Cordes pointed out in a comment below that using
// compare_exchange_weak() may be better suited for most
// uses.
// Then again, the needed strength of the exchange depends
// on your application, and the hardware it's intended to run on.
std::atomic<double> f{};
for (double g = f; !f.compare_exchange_strong(g, g + 1.0);)
;
return 0;
}