为什么 volatile 不是 sig_atomic_t 的一部分

Too*_*sie 5 c++ signals volatile

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

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

就像是:

using sig_atomic_t = volatile int;
Run Code Online (Sandbox Code Playgroud)

120*_*arm 4

这是从 C 继承的。C 定义虽然允许sig_atomic_tvolatile 限定,但并不要求它。我查看过的标准文档(N1570)中使用的所有示例均以volatile sig_atomic_t.

如今,在可行的情况下,使用标头std:atomic中指定的其他功能可能会更好。(另请参阅cppreference 上的sig_atomic_t。)<atomic>

  • 在可移植代码中,[不推荐]`std::atomic`(/sf/ask/3961927931/ multithreade/56600194#56600194) 用于信号处理程序,因为存在潜在的死锁风险。另外,但不太重要的是,默认的 seq/cst 使其比必要的更强大 (3认同)