Which Types on a 64-Bit Computer are Naturally Atomic in GNU C and GNU C ?
Unlike in C11 and later, or C 11 and later, which provide guarantees for atomicity using _Atomic or std::atomic types, the C and C language standards themselves make no guarantees regarding atomic reads and writes. Instead, the level of atomicity depends on the compiler implementation and the target architecture.
On a 64-bit computer running Linux on an x86-64 processor, the natural atomicity of different data types varies. The GNU C and GNU C compilers (gcc and g ) offer certain levels of atomicity for specific types.
Atomic Types:
-
int: The typedef _Atomic_word in the header indicates that int is atomic.
-
__sig_atomic_t: The typedef in the header defines an atomic integer type, ensuring atomic reads and writes in the presence of asynchronous interrupts.
Non-Atomic Types:
- int64_t (long long)
- uint64_t (unsigned long long)
- double (64-bits)
- long double (also 64-bits)
Additional Notes:
- Atomicity refers to atomic reads and writes, not atomic increment, decrement, or compound assignment.
- It's important to note that while certain types are guaranteed to be atomic in certain contexts (e.g., volatile sig_atomic_t is atomic with respect to signals), they are not necessarily atomic with respect to threads.
- The compiler may optimize code in ways that break assumptions about atomicity, especially if those assumptions are not explicitly stated in the language standards.
- To ensure thread-safe concurrent access, explicit use of _Atomic or std::atomic types is recommended.
The above is the detailed content of Which Data Types Are Naturally Atomic on a 64-Bit GNU C/C System?. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn