volatile varible

From: http://en.wikipedia.org/wiki/Volatile_variable#In_C_and_C.2B.2B

 

In C and C++

In C, and consequently C++, the volatile keyword was intended to[ 1]

  • allow access to memory mapped devices
  • allow uses of variables between setjmp and longjmp
  • allow uses of sig_atomic_t variables in signal handlers

Operations on volatile variables are not atomic , nor do they establish a proper happens-before relationship for threading. This is according to the relevant standards (C, C++, POSIX, WIN32), and this is the matter of fact for the vast majority of current implementations. The volatile keyword is basically worthless as a portable threading construct.[ 2] [ 3] [ 4] [ 5] [ 6]

Example of MMIO In C

In this example, the code sets the value stored in foo to 0 . It then starts to poll that value repeatedly until it changes to 255 :

static
 int
 foo;


void bar( void ) {
foo = 0 ;

while ( foo != 255)
;
}

An optimizing compiler will notice that no other code can possibly change the value stored in foo , and will assume that it will remain equal to 0 at all times. The compiler will therefore replace the function body with an infinite loop similar to this:

void
 bar_optimized(
void
)
 {

foo = 0 ;

while ( true )
;
}

However, foo might represent a location that can be changed by other elements of the computer system at any time, such as a hardware register of a device connected to the CPU . The above code would never detect such a change; without the volatile keyword, the compiler assumes that the current program is the only part of the system that could change the value (which is by far the most common situation).

To prevent the compiler from optimizing code as above, the volatile keyword is used:

static
 volatile
 int
 foo;


void bar ( void ) {
foo = 0 ;

while ( foo != 255)
;
}

With this modification the loop condition will not be optimized away, and the system will detect the change when it occurs.

Optimization comparison in C

The following C programs, and accompanying disassemblies, demonstrate how the volatile keyword affects the compiler's output. The compiler in this case was GCC .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值