My question is quite simple. Why isn't std::atomic<double>
implemented completely? I know it has to do with atomic RMW (read-modify-write) access. But I really don't see, why this shouldn't be possible on a double
.
It's specified that any trivially copyable type can be used. And of course double
is among them. So C++11 requires the basic operations (load, store, CAS, exchange, etc.) that you can use with any class type.
However, on integers an extra set of operations is possible (fetch_add
, ++
, +=
, etc).
A double
differs very little from these types. It's native, trivially copyable, etc. Why didn't the standard include the double
with these types?
Update: C++20 does specialize std::atomic<T>
for floating-point types, with fetch_add and sub. C++20 std::atomic<float>- std::atomic<double>.specializations But not atomic absolute-value (AND) or negate (XOR).
Editor's note: Without C++20 you can roll your own out of CAS; see Atomic double floating point or SSE/AVX vector load/store on x86_64 for portable examples; atomic<double>
and float are lock-free on most C++ implementations.
double
operations. So how would you implement it? – Angew is no longer proud of SOstd::atomic<double>
is lock-free on gcc/clang/msvc. stackoverflow.com/questions/45055402/… – Peter CordesInterlockedXor64
– Peter Cordes