C语言中的原子类型是什么? [英] What are atomic types in the C language?

查看:103
本文介绍了C语言中的原子类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I remember I came across certain types in the C language called atomic types, but we have never studied them.

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

So, how do they differ from regular types like int,float,double,long etc., and what are their uses?

推荐答案

原子类型是保证在单个指令中进行读取和写入的类型.来自 gnu.org 的更多解释:

Atomic types are those for which reading and writing are guaranteed to happen in a single instruction. More explanation from gnu.org:

为了避免中断访问变量的不确定性,您可以使用访问始终是原子的特定数据类型:sig_atomic_t.保证读取和写入此数据类型发生在单个指令中,因此处理程序无法运行访问的中间".

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.

类型 sig_atomic_t 总是一个整数数据类型,但它是哪个是,以及它包含多少位,可能因机器而异.

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.

数据类型:sig_atomic_t
这是整数数据类型.本的对象类型总是以原子方式访问.

Data Type: sig_atomic_t
This is an integer data type. Objects of this type are always accessed atomically.

在实践中,您可以假设 int 是原子的.你也可以假设指针类型是原子的;那很方便.这两个假设在 GNU C 库支持的所有机器和我们所知的所有 POSIX 系统上都是正确的.

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(无从属关系).

For even more detail and some C11-specific stuff, check out CppReference.com (no affiliation).

这篇关于C语言中的原子类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆