Unlike language standards, GNU C and GNU C compilers treat atomicity based on specific architectures. While C11 and C 11 introduce _Atomic types and std::atomic<> types respectively, this article focuses solely on naturally atomic reads and writes, excluding atomic increment, decrement, or compound assignment.
On a 64-bit Linux computer with an x86-64 processor, the following types have naturally atomic reads and writes:
However, it's important to note that even these types are not definitively automatically atomic according to the language standards.
There are two main senses of "atomic":
Just because a type is naturally atomic at the hardware level does not mean that the compiler will always use atomic instructions to access it. Optimizations may lead to non-atomic access, even for data types that are known to be atomic on the target hardware.
For example, a load from a 32-bit integer on x86 is atomic, but a compiler may use a 16-bit partial load or store that is not guaranteed to be atomic.
In summary, there are no types in C or C that are definitively automatically atomic on a 64-bit computer. To ensure atomic access, it is crucial to use _Atomic or std::atomic types or rely on documentation to verify atomic guarantees for specific architectures and compilers.
The above is the detailed content of Which C and C Types Exhibit Naturally Atomic Behavior on a 64-Bit x86-64 System?. For more information, please follow other related articles on the PHP Chinese website!