今天看libev代码,看到# define EV_ATOMIC_T sig_atomic_t volatile,由于对sig_atomic_t 类型不熟悉,查了一下。把查到的信息记录下来。
sig_atomic_t 类型指的是只需一条指令完成读写的数据类型,所以不可能是结构体,在linux下就是int类型。
转载地址:http://www.cnblogs.com/GoodGoodWorkDayDayUp/archive/2011/05/14/2046082.html 《volatile 和 sig_atomic_t》
1).volatile
a. volatile是C语言定义的关键字,gcc为了需要又定义了__volatile__,它和
volatile表达的是同一意思。
b. volatile的本意是"易变的",由于访问寄存器的速度快于访存,所以编译器一般
都会作优化以减少访存。如果变量加上volatile修饰,则编译器就不会对此变量
的读写操作进行优化,即不通过寄存器缓冲而直接访存。
c. __asm__ __volatile__一起指示编译器不要改动优化后面的汇编语句。
http://www.gnu.org/s/hello/manual/libc/Atomic-Types.html
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.
This is an integer data type. Objects of this type are always accessed atomically.
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.
本文详细解析了sig_atomic_t和volatile的含义及其应用场景,包括它们如何确保数据访问的原子性和防止编译器优化,特别强调了在信号处理、多线程及硬件交互中的重要性。
886

被折叠的 条评论
为什么被折叠?



