c - C语言中的原子类型是什么?

标签 c types atomic

我记得我在 C 语言中遇到过某些称为原子类型的类型,但我们从未研究过它们。

那么,它们与 intfloatdoublelong 等常规类型有何不同。 ,它们的用途是什么?

最佳答案

原子类型是保证在一条指令中进行读取和写入的类型。更多解释来自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/

相关文章:

c - 如何找出哪些文件与 "-lc"链接?

c++ - 如何在 C/C++ 中对二维数组求和

c - 从图像中删除白色背景

c - C 和 gcc 中原子变量的相等性测试

linux - 写入 SOCK_SEQPACKET 是原子的吗?

rest - 使用 REST 避免重复的 POST

c - setuid 或 setgid 失败

javascript - Typescript:忽略与基本类型并行的自定义类型

function - 相似括号函数类型之间的差异

使用泛型类型或接口(interface)的 C# 集合