atomic integer operations P176

理解Linux内核中atomic_t类型的使用
本文详细解析了Linux内核使用atomic_t类型的原因,包括原子操作的重要性、避免编译器优化的方法以及如何避免CPU体系结构问题。通过深入探讨C标准中关于结构体元素顺序的内容,解释了为何使用结构体可以实现这一目的。
typedef struct {
        int counter;
} atomic_t;

#ifdef CONFIG_64BIT
typedef struct {
        long counter;
} atomic64_t;
#endif

linux 内核使用特殊的数据类型atomic_t来代替直接使用C语言的整型,是出自以下几个原因:

(1)关于atomic_t的函数只接受atomic_t类型

(2)使用atomic_t类型可以避免编译器的优化,原子操作对于使用正确的内存地址很重视。

(3)可以避免CPU的体系结构的问题

 

这里就有个问题,为什么使用结构体就可以避免原因(2),编译器没有优化之,

google了下,得出。

 

gcc does not reorder the elements of a struct, because that would violate the C standard. Section 6.7.2.1 of the C99 standard states:

Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared.

转载于:https://www.cnblogs.com/lxgeek/archive/2012/03/30/2426137.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值