您的位置:首页 > 编程语言 > C语言/C++

C语言的那些小秘密——volatile(转bigloomy)

2012-07-28 10:38 232 查看
volatile的重要性对于搞嵌入式的程序员来说是不言而喻的,对于volatile的了解程度常常被不少公司在招聘嵌入式编程人员面试的时候作为衡量一个应聘者是否合格的参考标准之一,为什么volatile如此的重要呢?这是因为嵌入式的编程人员要经常同中断、底层硬件等打交道,而这些都用到volatile,所以说嵌入式程序员必须要掌握好volatile的使用。

其实就象读者所熟悉的const一样,volatile是一个类型修饰符。在开始讲解volatile之前我们先来讲解下接下来要用到的一个函数,知道如何使用该函数的读者可以跳过该函数的讲解部分。

原型:int gettimeofday ( struct timeval * tv , struct timezone * tz );

头文件:#include <sys/time.h>

功能:获取当前时间

返回值:如果成功返回0,失败返回-1,错误代码存于errno中。

gettimeofday()会把目前的时间用tv所指的结构返回,当地时区的信息则放到tz所指的结构中。

[cpp]
view plaincopyprint?

timeval结构定义为:  
struct timeval{  
    long tv_sec;   

    long tv_usec;   

};  
timezone 结构定义为:  
struct timezone{  

    int tz_minuteswest;   

    int tz_dsttime;   

};  

[cpp]
view plaincopyprint?

timeval结构定义为:  
struct timeval{  
    long tv_sec;   
    long tv_usec;   
};  
timezone 结构定义为:  
struct timezone{  
    int tz_minuteswest;   
    int tz_dsttime;   
};  

[cpp]
view plaincopyprint?

#include <stdio.h>   

#include <sys/time.h>   

  
int main(int argc, char * argv[])  

{  
    struct timeval start,end;  

    gettimeofday( &start, NULL );  /*测试起始时间*/  

    double timeuse;  

    int j;  

    for(j=0;j<1000000;j++)  

        ;  
    gettimeofday( &end, NULL );   /*测试终止时间*/  

    timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_sec - start.tv_sec ;  

    timeuse /= 1000000;  
printf("运行时间为:%f\n",timeuse);  

  
    return 0;  

  
}  

[cpp]
view plaincopyprint?

#include <stdio.h>   
#include <sys/time.h>   
  
int main(int argc, char * argv[])  
{  
    struct timeval start,end;  
    gettimeofday( &start, NULL );  /*测试起始时间*/  
    double timeuse;  
    int j;  
    for(j=0;j<1000000;j++)  
        ;  
    gettimeofday( &end, NULL );   /*测试终止时间*/  
    timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_sec - start.tv_sec ;  
    timeuse /= 1000000;  
printf("运行时间为:%f\n",timeuse);  
  
    return 0;  
  
}  

#include <stdio.h>
#include <sys/time.h>

int main(int argc, char * argv[])
{
struct timeval start,end;
gettimeofday( &start, NULL );  /*测试起始时间*/
double timeuse;
int j;
for(j=0;j<1000000;j++)
;
gettimeofday( &end, NULL );   /*测试终止时间*/
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_sec - start.tv_sec ;
timeuse /= 1000000;
printf("运行时间为:%f\n",timeuse);

return 0;

}
运行结果为:

[cpp]
view plaincopyprint?

root@ubuntu:/home# ./p  
运行时间为:0.002736  

[cpp]
view plaincopyprint?

root@ubuntu:/home# ./p  
运行时间为:0.002736  

[cpp]
view plaincopyprint?

#include <stdio.h>   

  
void main()  

{  
int a=12;  

printf("a的值为:%d\n",a);  

__asm {mov dword ptr [ebp-4], 0h}  
int b = a;  
printf("b的值为:%d\n",b);  

}  

[cpp]
view plaincopyprint?

#include <stdio.h>   
  
void main()  
{  
int a=12;  
printf("a的值为:%d\n",a);  
__asm {mov dword ptr [ebp-4], 0h}  
int b = a;  
printf("b的值为:%d\n",b);  
}  

#include <stdio.h>

void main()
{
int a=12;
printf("a的值为:%d\n",a);
__asm {mov dword ptr [ebp-4], 0h}
int b = a;
printf("b的值为:%d\n",b);
}
先分析下上面的代码,我们使用了一句__asm {mov dword ptr [ebp-4], 0h}来修改变量a在内存中的值,如果有对这句代码功能不清楚的读者可以参考我之前的一篇《C语言的那些小秘密之堆栈》,在此就不做过多的讲解了。前面已经讲解了Debug和Release 编译方式的区别,那么我们现在来对比看下结果。注:使用vc6编译运行,如无特殊说明,均在linux环境下编译运行。读者自己在编译的时候别忘了选择编译运行的模式。

使用Debug模式的结果为:

[cpp]
view plaincopyprint?

a的值为:12  
b的值为:0  
Press any key to continue  

[cpp]
view plaincopyprint?

a的值为:12  
b的值为:0  
Press any key to continue  

[cpp]
view plaincopyprint?

a的值为:12  
b的值为:12  
Press any key to continue  

[cpp]
view plaincopyprint?

a的值为:12  
b的值为:12  
Press any key to continue  

a的值为:12
b的值为:12
Press any key to continue
看看上面的运行结果我们发现在Release模式进行了优化之后b的值为了12,但是使用Debug模式的时候b的值为0。为什么会出现这样的情况呢?我们先不说答案,再来看看下面一段代码。注:使用vc6编译运行

[cpp]
view plaincopyprint?

#include <stdio.h>   

  
void main()  

{  
int volatile a=12;  

printf("a的值为:%d\n",a);  

__asm {mov dword ptr [ebp-4], 0h}  
int b = a;  
printf("b的值为:%d\n",b);  

}  

[cpp]
view plaincopyprint?

#include <stdio.h>   
  
void main()  
{  
int volatile a=12;  
printf("a的值为:%d\n",a);  
__asm {mov dword ptr [ebp-4], 0h}  
int b = a;  
printf("b的值为:%d\n",b);  
}  

[cpp]
view plaincopyprint?

a的值为:12  
b的值为:0  
Press any key to continue  

[cpp]
view plaincopyprint?

a的值为:12  
b的值为:0  
Press any key to continue  

a的值为:12
b的值为:0
Press any key to continue
使用Release模式的结果为:

[cpp]
view plaincopyprint?

a的值为:12  
b的值为:0  
Press any key to continue  

[cpp]
view plaincopyprint?

a的值为:12  
b的值为:0  
Press any key to continue  

[cpp]
view plaincopyprint?

#include <stdio.h>   

#include <sys/time.h>   

  
int main(int argc, char * argv[])  

{  
    struct timeval start,end;  

    gettimeofday( &start, NULL );  /*测试起始时间*/  

    double timeuse;  

    int j;  

    for(j=0;j<10000000;j++)  

        ;  
    gettimeofday( &end, NULL );   /*测试终止时间*/  

    timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec -start.tv_usec;  

    timeuse /= 1000000;  
printf("运行时间为:%f\n",timeuse);  

  
    return 0;  

  
}  

[cpp]
view plaincopyprint?

#include <stdio.h>   
#include <sys/time.h>   
  
int main(int argc, char * argv[])  
{  
    struct timeval start,end;  
    gettimeofday( &start, NULL );  /*测试起始时间*/  
    double timeuse;  
    int j;  
    for(j=0;j<10000000;j++)  
        ;  
    gettimeofday( &end, NULL );   /*测试终止时间*/  
    timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec -start.tv_usec;  
    timeuse /= 1000000;  
printf("运行时间为:%f\n",timeuse);  
  
    return 0;  
  
}  

#include <stdio.h>
#include <sys/time.h>

int main(int argc, char * argv[])
{
struct timeval start,end;
gettimeofday( &start, NULL );  /*测试起始时间*/
double timeuse;
int j;
for(j=0;j<10000000;j++)
;
gettimeofday( &end, NULL );   /*测试终止时间*/
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec -start.tv_usec;
timeuse /= 1000000;
printf("运行时间为:%f\n",timeuse);

return 0;

}
与之前我们测试时间的代码一样,我们只是增大了for()循环的次数。

先来看看我们不使用优化的结果:

[cpp]
view plaincopyprint?

root@ubuntu:/home# gcc time.c -o p  
root@ubuntu:/home# ./p  
运行时间为:0.028260  

[cpp]
view plaincopyprint?

root@ubuntu:/home# gcc time.c -o p  
root@ubuntu:/home# ./p  
运行时间为:0.028260  

[cpp]
view plaincopyprint?

root@ubuntu:/home# gcc -o p time.c -O2  

root@ubuntu:/home# ./p  
运行时间为:0.000001  

[cpp]
view plaincopyprint?

root@ubuntu:/home# gcc -o p time.c -O2  
root@ubuntu:/home# ./p  
运行时间为:0.000001  

root@ubuntu:/home# gcc -o p time.c -O2
root@ubuntu:/home# ./p
运行时间为:0.000001
从结果显然可以看出差距如此之大,但是如果我们在上面的代码中修改一下int j为int  volatile j之后再来看看如下代码:

[cpp]
view plaincopyprint?

#include <stdio.h>   

#include <sys/time.h>   

  
int main(int argc, char * argv[])  

{  
    struct timeval start,end;  

    gettimeofday( &start, NULL );  /*测试起始时间*/  

    double timeuse;  

    int volatile j;  

    for(j=0;j<10000000;j++)  

        ;  
    gettimeofday( &end, NULL );   /*测试终止时间*/  

    timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec -start.tv_usec;  

    timeuse /= 1000000;  
printf("运行时间为:%f\n",timeuse);  

  
    return 0;  

  
}  

[cpp]
view plaincopyprint?

#include <stdio.h>   
#include <sys/time.h>   
  
int main(int argc, char * argv[])  
{  
    struct timeval start,end;  
    gettimeofday( &start, NULL );  /*测试起始时间*/  
    double timeuse;  
    int volatile j;  
    for(j=0;j<10000000;j++)  
        ;  
    gettimeofday( &end, NULL );   /*测试终止时间*/  
    timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec -start.tv_usec;  
    timeuse /= 1000000;  
printf("运行时间为:%f\n",timeuse);  
  
    return 0;  
  
}  

[cpp]
view plaincopyprint?

root@ubuntu:/home# gcc time.c -o p  
root@ubuntu:/home# ./p  
运行时间为:0.027647  

[cpp]
view plaincopyprint?

root@ubuntu:/home# gcc time.c -o p  
root@ubuntu:/home# ./p  
运行时间为:0.027647  

root@ubuntu:/home# gcc time.c -o p
root@ubuntu:/home# ./p
运行时间为:0.027647
使用了优化的运行结果为:

[cpp]
view plaincopyprint?

root@ubuntu:/home# gcc -o p time.c -O2  

root@ubuntu:/home# ./p  
运行时间为:0.027390  

[cpp]
view plaincopyprint?

root@ubuntu:/home# gcc -o p time.c -O2  
root@ubuntu:/home# ./p  
运行时间为:0.027390  

root@ubuntu:/home# gcc -o p time.c -O2
root@ubuntu:/home# ./p
运行时间为:0.027390
我们发现此时此刻不管是否使用优化语句运行,时间几乎没有变化,只是有微小的差异,这微小的差异是由于计算机本身所导致的。所以我们通过对于上面一个没有使用volatile和下面一个使用了volatile的对比结果可知,使用了volatile的变量在使用优化语句是for()循环并没有得到优化,因为for()循环执行的是一个空操作,那么通常情况下使用了优化语句使得这个for()循环被优化掉,根本就不执行。就好比编译器在编译的过程中将i的值设置为大于或者等于10000000的一个数,使得for()循环语句不会执行。但是由于我们使用了volatile,使得编译器就不会自作主张的去动我们的i值,所以循环体得到了执行。举这个例子的原因是要让读者牢记,如果我们定义了volatile变量,那么它就不会被编译器所优化。

当然volatile还有那些值得注意的地方呢?由于访问寄存器的速度要快过直接访问内存的速度,所以编译器一般都会作减少对于内存的访问,但是如果将变量加上volatile修饰,则编译器保证对此变量的读写操作都不会被优化。这样说可能有些抽象了,再看看下面的代码,在此就简要的写出几步了。

main()

{

        int i=o;

        while(i==0)

        {

                 ……

        }

}

分析以上代码,如果我们没有在while循环体结构里面改变i的值,编译器在编译的过程中就会将i的值备份到一个寄存器中,每次执行判断语句时就从该寄存器取值,那么这将是一个死循环,但是如果我们做如下的修改:

main()

{

        int volatile i=o;

        while(i==0)

        {

                 ……

        }

}

我们在i的前面加上了一个volatile,假设while()循环体里面执行的是跟上一个完全一样的操作,但是这个时候就不能说是一个死循环了,因为编译器不会再对我们的i值进行"备份"操作了,每次执行判断的时候都会直接从i的内存地址中读取,一旦其值发生变化就退出循环体。

最后给出一点就是在实际使用中volatile的使用的场合大致有以下几点:

1、中断服务程序中修改的供其它程序检测的变量需要加volatile;

2、多任务环境下各任务间共享的标志应该加volatile;

3、存储器映射的硬件寄存器通常也要加volatile说明,因为每次对它的读写都可能有不同意义。

对于volatile的讲解我们到此就结束了。由于本人水平有限,博客中的不妥或错误之处在所难免,殷切希望读者批评指正。同时也欢迎读者共同探讨相关的内容,如果乐意交流的话请留下你宝贵的意见。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息