atomic integer operations P176

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.

posted @ 2012-03-30 22:31  lxgeek  阅读(327)  评论(0编辑  收藏  举报