什么是一个静态全局和静态volatile变量之间的区别? [英] What is the difference between a static global and a static volatile variable?

查看:666
本文介绍了什么是一个静态全局和静态volatile变量之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个静态全局变量,并在文件范围的静态volatile变量,

两者都被ISR和主回路和主回路检查该变量的值进行更新。

优化既不全局变量也不挥发变量期间这里进行了优化。因此代替使用挥发性变量的全局变量解决了这个问题。

因此​​,它是很好的使用全局变量,而不是动荡?

任何特别原因需要使用静态挥发?

任何示例程序将AP preciable。

在此先感谢..

I have used a static global variable and a static volatile variable in file scope,

both are updated by an ISR and a main loop and main loop checks the value of the variable.

here during optimization neither the global variable nor the volatile variable are optimized. So instead of using a volatile variable a global variable solves the problem.

So is it good to use global variable instead of volatile?

Any specific reason to use static volatile??

Any example program would be appreciable.

Thanks in advance..

推荐答案

他们是不同的东西。我不是挥发性语义学的专家。但我认为这是什么使这里介绍感。

They are different things. I'm not an expert in volatile semantics. But i think it makes sense what is described here.

全球仅仅意味着有问题的标识符在文件范围声明。有不同的范围,调用的函数(其中后藤标签中定义),网络连接文件(如全局居住),块(其中正常的局部变量所在),和函数原型(其中功能参​​数所在)。这个概念只是存在结构标识的知名度。它没有任何与优化。

Global just means the identifier in question is declared at file-scope. There are different scopes, called function (where goto-labels are defined in), file (where globals reside), block (where normal local variables reside), and function prototype (where function parameters reside). This concept just exist to structure the visibility of identifiers. It doesn't have anything to do with optimizations.

静态是一个存储时间(我们不会看这里)和一个方式给文件范围内部联动内声明的名称。这只能为一个翻译单元内所要求的功能或对象来完成。一个典型的例子可能是帮助打印出接受的参数函数,仅根据定义的函数调用在同一个 .C 文件。

static is a storage duration (we won't look at that here) and a way to give a name declared within file scope internal linkage. This can be done for functions or objects only required within one translation unit. A typical example might be a help function printing out the accepted parameters, and which is only called from the main function defined in the same .c file.

6.2.2 / 2 在C99草案:

如果一个文件范围的声明
  鉴定连接器对象或函数
  包含存储类特定连接器
  静态的,鉴定音响器内部有
  联动。

If the declaration of a file scope identifier for an object or a function contains the storage class specifier static, the identifier has internal linkage.

内部连接意味着该标识符不是当前转换单元(如帮助的上述功能)外部可见。

Internal linkage means that the identifier is not visible outside the current translation unit (like the help function of above).

挥发性是另一回事:( 6.7.3 / 6

Volatile is a different thing: (6.7.3/6)

这是具有挥发性,合格网络版的对象
  类型可能是未知的方式莫迪网络版
  实施或者有其他
  未知的副作用。因此,任何
  前pression参照这样一个对象
  应严格进行评估,根据
  到抽象机的规则,
  如5.1.2.3所述。此外,
  在每个序列点最后的价值
  存储在对象应同意
  即$ P $由抽象pscribed
  机,除了由教育署为改性音响
  未知因素提到
  previously。

An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3. Furthermore, at every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine, except as modified by the unknown factors mentioned previously.

本标准规定了一个例子一个很好的例子,其中挥发性是多余的( 5.1.2.3/8

The Standard provides an excellent example for an example where volatile would be redundant (5.1.2.3/8):

这是实现可以去网络连接定义一个
  之间的一对一的对应关系
  抽象与实际语义:在
  每个序列点,值
  实际的对象会同意
  这些特定网络由抽象编
  语义。关键字挥发性
  然后是多余的。

An implementation might define a one-to-one correspondence between abstract and actual semantics: at every sequence point, the values of the actual objects would agree with those specified by the abstract semantics. The keyword volatile would then be redundant.

序列点在哪里的副作用有关的抽象机的效果的完成点(如存储单元格的值即外部条件不包括在内)。右侧和左侧的&放大器;&安培; || ,在; 键,从函数调用返回例如是序列点。

Sequence points are points where the effect of side effects concerning the abstract machine are completed (i.e external conditions like memory cell values are not included). Between the right and the left of && and ||, after ; and returning from a function call are sequence points for example.

抽象语义的是编译器可以从特定的程序中看到code的唯一序列演绎。优化的效果是不相关的在这里。 实际语义的包括以书面形式向对象(例如,存储单元的改变)来完成的副作用的影响。排位对象作为挥发性是指一个总是从存储器获取一个对象的值直(由未知因素改性)。该标准并没有提及任何地方的线程,如果你必须依靠变化的顺序,或者在操作的原子,你应该使用依赖于平台的方式来保证。

The abstract semantics is what the compiler can deduce from seeing only the sequence of code within a particular program. Effects of optimizations are irrelevant here. actual semantics include the effect of side effects done by writing to objects (for example, changing of memory cells). Qualifying an object as volatile means one always gets the value of an object straight from memory ("as modified by the unknown factors"). The Standard doesn't mention threads anywhere, and if you must rely on the order of changes, or on atomicity of operations, you should use platform dependent ways to ensure that.

有关一个易于理解概述,英特尔有一个关于它的伟大的文章<一个href=\"http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/\">here.

For an easy to understand overview, intel has a great article about it here.

请声明你文件范围(全局)数据波动。本身全局数据并不意味着变量的值将等于存储在存储器中的值。和静也只会让你的对象本地当前转换单元(当前 .C 文件,并通过它include'ed所有其他文件#)。

Keep declaring your file-scope (global) data as volatile. Global data in itself does not mean the variables' value will equal to the value stored in memory. And static does only make your objects local to the current translation unit (the current .c files and all other files #include'ed by it).

这篇关于什么是一个静态全局和静态volatile变量之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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