为什么没有完全实现原子双重 [英] Why isn't atomic double fully implemented
问题描述
我的问题很简单.为什么std::atomic<double>
完全实现?我知道这与原子RMW(读-修改-写)访问有关.但我真的不明白,为什么在double
上不可能做到这一点.
已指定可以使用任何普通可复制类型.当然,double
就在其中.因此,C ++ 11要求您可以对任何类类型使用的基本操作(加载,存储,CAS,交换等).
但是,在整数上可以进行一组额外的操作(fetch_add
,++
,+=
等).
A double
与这些类型的区别很小.它是本机的,可复制的,等等.为什么标准不包括这些类型的double
?
更新:C ++ 20确实将fc_add和sub专用于std::atomic<T>
用于浮点类型. C ++ 20 std :: atomic< float>-std :: atomic< double> .specializations 但不是原子绝对值(AND)或取反(XOR).
编者注:如果没有C ++ 20,您可以将自己的软件从CAS中移出.参见原子双浮点或SSE/AVX向量在x86_64上加载/存储(用于可移植示例); atomic<double>
和float在大多数C ++实现中都是无锁的.
std::atomic<double>
,它可以在C ++ 11的规则下工作.您可以执行加载和存储操作,并进行比较交换等.
该标准规定仅对整数类型"的原子提供算术运算(+,*,+ =,&等),因此std::atomic<double>
不会定义任何这些运算./p>
我的理解是,由于当今使用的硬件几乎不支持对浮点类型进行fetch-add或任何其他原子算术运算的支持,因此C ++标准未为其提供运算符,因为它们必须是实施效率低下.
(编辑).顺便说一句,VS2015RC中的std::atomic<double>
是无锁的.
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.
std::atomic<double>
is supported in the sense that you can create one in your program and it will work under the rules of C++11. You can perform loads and stores with it and do compare-exchange and the like.
The standard specifies that arithmetic operations (+, *, +=, &, etc.) are only provided for atomics of "integral types", so an std::atomic<double>
won't have any of those operations defined.
My understanding is that, because there is little support for fetch-add or any other atomic arithmetic operations for floating point types in hardware in use today, the C++ standard doesn't provide the operators for them because they would have to be implemented inefficiently.
(edit). As an aside, std::atomic<double>
in VS2015RC is lock-free.
这篇关于为什么没有完全实现原子双重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!