使用挥发性? [英] use of volatile ?

查看:107
本文介绍了使用挥发性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以给出一个使用volatile关键字的例子吗?

can any one give an example which use volatile keyword ?

推荐答案

pa **************** @ gmail.com 写道:
pa****************@gmail.com wrote:

可以给任何一个使用volatile关键字的例子吗?
can any one give an example which use volatile keyword ?



Voila:


#include< signal.h>


volatile sig_atomic_t bromine;


你真的想知道什么,为什么?


-

克里斯小步骤 Dollin


Hewlett-Packard Limited注册办公室:Cain Road,Bracknell,

注册号:690597 England Berks RG12 1HN

Voila:

#include <signal.h>

volatile sig_atomic_t bromine;

What did you really want to know, and why?

--
Chris "small steps" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


一个案例是仅在中断服务程序中访问的变量。

--- Matthew Hicks

One case is for the variables only accessed inside an interrupt service routine.
---Matthew Hicks


任何人都可以给出一个使用volatile关键字的例子吗?
can any one give an example which use volatile keyword ?





< ; pa **************** @ gmail.comha scritto nel messaggio新闻:11 ********************** @ i38g2000prf.googlegr oups.com ...

<pa****************@gmail.comha scritto nel messaggio news:11**********************@i38g2000prf.googlegr oups.com...

任何人都可以给出一个使用volatile关键字的例子吗?
can any one give an example which use volatile keyword ?



它用于声明一个可能在

程序后面发生变化的变量,所以程序必须在抽象的

机器时才能访问它。 />

例如:

# include< time.h>

double measure_time(int(* func)(int),int min,int max)

{

clock_t t0,t1;

volatile int res;

int i;

double el0,el1;

t0 = clock();

for(i = min;我< = max; i ++)

res = func(i);

t1 = clock();

el0 =(t0-t1)/((double )max - min + 1)/ CLOCKS_PER_SEC;

t0 = clock();

for(i = min; i< = max; i ++)

res = 0;

t1 = clock();

el1 =(t0 - t1)/((double)max - min + 1)/ CLOCKS_PER_SEC ;

返回el0 - el1;

/ *第一个循环所需的时间,包括函数调用,

*减去所需的时间循环本身* /

}


如果res不易变,并且func()没有副作用,

编译器可以自由地优化掉除了最后一个之外的所有对func()

的调用,因为它们将是无用的并且没有严格的符合程序的b $ b b可以确定它们实际上并没有执行

It serves to declare a variable which could change behind the
program''s back, so the program must access it whenever the abstract
machine does.

For example:
#include <time.h>
double measure_time(int (*func)(int), int min, int max)
{
clock_t t0, t1;
volatile int res;
int i;
double el0, el1;
t0 = clock();
for (i = min; i <= max; i++)
res = func(i);
t1 = clock();
el0 = (t0 - t1) / ((double)max - min + 1) / CLOCKS_PER_SEC;
t0 = clock();
for (i = min; i <= max; i++)
res = 0;
t1 = clock();
el1 = (t0 - t1) / ((double)max - min + 1) / CLOCKS_PER_SEC;
return el0 - el1;
/* the time needed for the first loop including function calls,
* minus the time needed for the loop itself */
}

If res weren''t volatile, and func() had no side effect, the
compiler would be free to optimize away all the calls to func()
except the last one, as they would be useless and no strictly
conforming program could determine that they weren''t actually
executed.


这篇关于使用挥发性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆