为什么 volatile 不是 sig_atomic_t 的一部分 [英] Why is volatile not part of sig_atomic_t

查看:29
本文介绍了为什么 volatile 不是 sig_atomic_t 的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的平台(X86、Fedora、gcc 9.1.1)上,sig_atomic_t 被定义为一个普通的 int.

On my platform (X86, Fedora, gcc 9.1.1) sig_atomic_t is typedef'd to a plain int.

在 C++ 标准中,sig_atomic_t 总是与 volatile 限定符一起使用.
我明白为什么需要 volatile ,但为什么它不是类型的一部分呢?

In the C++ standard, sig_atomic_t is always used with the volatile qualifier.
I understand why volatile is needed, but why is it not part of the type then ?

类似于:

using sig_atomic_t = volatile int;

推荐答案

C89 说它是

可以作为原子访问的对象的整数类型实体,即使存在异步中断.

the integral type of an object that can be accessed as an atomic entity, even in the presence of asynchronous interrupts.

未指定 volatile,可能是因为在第一个标准制定时限定符是一个新事物.

volatile not specified, probably because qualifiers were a new thing when the first standard was made.

C99 添加了可能是 volatile 限定的.

C99 adds "possibly volatile-qualified.

我想从那时起它就向后兼容并结合了没人关心",因为信号处理是大多数项目中相对较小的部分.

I suppose it's backwards compatibility from then on combined with "nobody cares enough", since signal-handling is a relatively minor part of most project.

也有人可能在不需要 volatile 的上下文中使用它(例如,存储用于与信号处理程序通信的标志的副本)和在非 GNU C(再次,向后兼容)中基本上不可能将一个类型映射到该类型的较少限定版本,这使得选择省略限定符的实现更加灵活.

Also somebody could presumably use it in a context where volatile is not required (e.g., to store a copy of a flag used for communication with signal handlers) and in non-GNU C (again, backwards compatibility) it's basically impossible to map a type to a less qualified version of that type, which makes an implementation that chooses to omit the qualifier more flexible.

这篇关于为什么 volatile 不是 sig_atomic_t 的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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