Home >Backend Development >C++ >Does x86_64 Support Atomic Operations on Doubles and SSE/AVX Vectors?
Although C 11 supports lock-free std::atomic
x86_64 supports the following atomic operations on doubles, performed using lock-free instructions:
Unfortunately, there is no way to guarantee the atomicity of 128b or 256b vector stores or loads across the cache coherency system. However, for aligned vectors, you can safely use vector loads and stores on shared double arrays without risk of tearing.
If atomic 16B loads are required, your only option is to use lock cmpxchg16b with desired=expected. if it succeeds, it replaces the existing value with itself. If it fails, you get the old contents. Note that this "load" faults on read-only memory, so use caution when passing pointers to functions that perform this operation.
Atomic 16B stores and RMW can both use lock cmpxchg16b in the obvious way. This makes pure stores much more expensive than regular vector stores, especially with multiple cmpxchg16b retries. However, atomic RMW is already expensive.
You can atomically update 16B objects but read the 8B halves separately. However, compilers do not provide a clean way to express this, and inlining cmpxchg16b is unreliable due to ongoing considerations by compiler developers.
The above is the detailed content of Does x86_64 Support Atomic Operations on Doubles and SSE/AVX Vectors?. For more information, please follow other related articles on the PHP Chinese website!