我记得我在 C 语言中遇到过某些称为原子类型的类型,但我们从未研究过它们。
那么,它们与 int
、float
、double
、long
等常规类型有何不同。 ,它们的用途是什么?
最佳答案
原子类型是保证在一条指令中进行读取和写入的类型。更多解释来自gnu.org :
24.4.7.2 Atomic Types
To avoid uncertainty about interrupting access to a variable, you can use a particular data type for which access is always atomic:
sig_atomic_t
. Reading and writing this data type is guaranteed to happen in a single instruction, so there’s no way for a handler to run “in the middle” of an access.The type
sig_atomic_t
is always an integer data type, but which one it is, and how many bits it contains, may vary from machine to machine.Data Type:
sig_atomic_t
This is an integer data type. Objects of this type are always accessed atomically.In practice, you can assume that
int
is atomic. You can also assume that pointer types are atomic; that is very convenient. Both of these assumptions are true on all of the machines that the GNU C Library supports and on all POSIX systems we know of.
有关更多详细信息和一些特定于 C11 的内容,请查看 CppReference.com (无从属关系)。
关于c - C语言中的原子类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36955884/