本文介绍了如何sig_atomic_t实际工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编译器或操作系统sig_atomic_t型和正常int型变量加以区分,并确保操作是原子?使用这两种方案都有相同的汇编code。如何额外小心,使操作原子?

How does the compiler or OS distinguish between sig_atomic_t type and normal int type variable, and ensures that the operation will be atomic? Programs using both have same assembler code. How extra care is taken to make the operation atomic?

推荐答案

sig_atomic_t 不是一个原子的数据类型。它只是你被允许在信号处理程序的上下文中使用的数据类型,这是所有。因此,更好地阅读名为原子相对的信号处理。

sig_atomic_t is not an atomic data type. It is just the data type that you are allowed to use in the context of a signal handler, that is all. So better read the name as "atomic relative to signal handling".

要保证与和从信号处理程序通信,只有原子数据类型的属性之一是必要的,即读取和更新总会看到一个一致的价值的事实。其他数据类型(如也许长长)可以与几个汇编指令编写了较低和较高的部分,例如 sig_atomic_t 是保证读写一气呵成。

To guarantee communication with and from a signal handler, only one of the properties of atomic data types is needed, namely the fact that read and update will always see a consistent value. Other data types (such as perhaps long long) could be written with several assembler instructions for the lower and higher part, e.g. sig_atomic_t is guaranteed to be read and written in one go.

因此​​,一个平台,可以选择任意整数基本类型为 sig_atomic_t 中它能够使保证挥发性sig_atomic_t 可在信号处理程序安全地使用。很多平台选择了 INT 对于这一点,因为他们知道,他们 INT 中写入一个指令。

So a platform may chose any integer base type as sig_atomic_t for which it can make the guarantee that volatile sig_atomic_t can be safely used in signal handlers. Many platforms chose int for this, because they know that for them int is written with a single instruction.

最新的C标准,C11,拥有原子类型,但它是一个完全不同的事情。其中一些(那些lockfree)也可以在信号处理程序中使用,但是这又是一个完全不同的故事。

The latest C standard, C11, has atomic types, but which are a completely different thing. Some of them (those that are "lockfree") may also be used in signal handlers, but that again is a completely different story.

这篇关于如何sig_atomic_t实际工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:37