变得不稳定 [英] cast to volatile

查看:88
本文介绍了变得不稳定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


甚至(或因为?)阅读标准(ISO / IEC 9899/1999)我没有

了解volatile的一些问题。后台嵌入了

编程,在主程序流和

中断之间交换数据。


在我工作的组织中,而不是声明变量volatile,

它在必要时被转换为volatile:


示例1(阅读):

int旗; //全局变量,非易失性

while((volatile)flag)//在读取之前转换为volatile

;


原因是:当变量声明为volatile时,

编译器根本无法对其进行优化。但是,通过使用强制转换为
volatile,可以强制某些区域的易失性行为,而其他区域(例如在中断中)仍然可以进行优化。另外,即使变量声明不在我的控制范围内,也可以使用



问题:标准涵盖了这个吗?


因为volatile的整个主题非常复杂(例如它与管道相关的是b $ b),有人知道一篇好的文章或书籍,它涵盖了

这个话题深入吗?


BTW,有没有人知道如何谷歌表达式(易变)

(*包括*括号)?


谢谢,


Uli G. Margull


nospam @" lastname" .de

Hello,

even (or because?) reading the standard (ISO/IEC 9899/1999) I do not
understand some issues with volatile. The background is embedded
programming where data is exchanged between main program flow and
interrupts.

At the organisation I work, instead of declaring a variable volatile,
it is casted to volatile when necessary:

Example 1 (reading):
int flag; // global variable, non-volatile
while( (volatile)flag ) // cast to volatile before reading
;

The reason for this is: when the variable is declared volatile, the
compiler cannot optimize it at all. However, by using the cast to
volatile, one can force volatile behavior in some areas, while others
(e.g. in the interrupt) still can be optimized. Addionally, this can be
used even if the variable declaration is not under my control.

Question: is this covered by the standard?

Since the whole topic of volatile is very complex (e.g. it is related
to pipeling), does somebody know a good article or book which covers
this topic deeply?

BTW, does anybody know how to google an expression like (volatile)
(*including* the bracket)?

Thanks,

Uli G. Margull

nospam@"lastname".de

推荐答案

去*************** @ margull.de 写道:
go***************@margull.de writes:
你好,

甚至(或因为?)阅读标准(ISO / IEC 9899/1999)我不了解volatile的一些问题。背景是嵌入式编程,在主程序流和中断之间交换数据。

在我工作的组织中,而不是声明变量volatile,
它在必要时被转换为易失性:

示例1(阅读):
int flag; //全局变量,非易失性
while((volatile)标志)//在读取前转换为volatile
;

原因是:声明变量时volatile,
编译器根本无法优化它。但是,通过使用强制转换为易失性,可以在某些区域强制执行易失性行为,而其他区域(例如在中断中)仍然可以进行优化。另外,即使变量声明不在我的控制之下,也可以使用它。


这种方法并不能做你想要的,也就是说它不会强迫易变的语义。改为使用它:


while(*(volatile *)& flag)...


问题:标准涵盖了这个问题?


是的,虽然可能不像它那样直接。


因为volatile的整个主题非常复杂(例如它是相关的<有人知道一篇很好的文章或书籍,深入讨论这个话题吗?
Hello,

even (or because?) reading the standard (ISO/IEC 9899/1999) I do not
understand some issues with volatile. The background is embedded
programming where data is exchanged between main program flow and
interrupts.

At the organisation I work, instead of declaring a variable volatile,
it is casted to volatile when necessary:

Example 1 (reading):
int flag; // global variable, non-volatile
while( (volatile)flag ) // cast to volatile before reading
;

The reason for this is: when the variable is declared volatile, the
compiler cannot optimize it at all. However, by using the cast to
volatile, one can force volatile behavior in some areas, while others
(e.g. in the interrupt) still can be optimized. Addionally, this can be
used even if the variable declaration is not under my control.
This method doesn''t do what you want, which is to say it doesn''t
force volatile semantics. Use this instead:

while( *(volatile*) &flag ) ...

Question: is this covered by the standard?
Yes, although perhaps not as directly as it might.

Since the whole topic of volatile is very complex (e.g. it is related
to pipeling), does somebody know a good article or book which covers
this topic deeply?




一小部分使用volatile的规则:


1.对可能意外改变的变量使用volatile。


2.在使用<的例程中使用volatile作为自动变量br />
setjmp()。


3.要强制特定访问的易失性语义,请获取变量的

地址并进行转换它是(volatile WHATEVER *),

解除引用表达式以获取值。


4.有时volatile是解决问题的合理方法

代码生成中的代码生成符合某些领域的问题

问题,例如x86上的gcc编译器带有

分配或castin的语义g加倍。不要这么做是偶然的,因为如果它是不必要的代码

质量很可能会下降。


5.除非你真的知道自己在做什么以及为什么要这样做,否则如果你使用的是挥发性的,你很可能会这样做。

有问题。试着找到解决

问题的另一种方法,如果你仍然需要使用易变的代码

一个很好的小例子并发布到comp.lang.c和

请求有用的建议。



A small set of rules for using volatile:

1. Use volatile for variables that might change "unexpectedly".

2. Use volatile for automatic variables in routines that use
setjmp().

3. To force volatile semantics on a particular access, take the
address of the variable and cast it to (volatile WHATEVER *),
dereferencing the cast expression to get the value.

4. Sometimes volatile is a reasonable way to get around problems
with code generation in compilers that have conformance
problems in some areas, eg, the gcc compiler on x86 with
semantics of assigning or casting to double. Don''t do
this just haphazardly, since if it''s unnecessary code
quality will very likely go down.

5. Unless you really know what you''re doing and why you''re
doing it, if you''re using volatile you''re likely doing
something wrong. Try to find another way to solve the
problem, and if you still have to use volatile code
up a nice small example and post to comp.lang.c and
ask for helpful suggestions.


Tim Rentsch< tx*@alumnus.caltech.edu>写道:
Tim Rentsch <tx*@alumnus.caltech.edu> writes:
这种方法并不是你想要的,也就是说它不会强制使用易失性语义。改为使用它:

while(*(volatile *)& flag)...
This method doesn''t do what you want, which is to say it doesn''t
force volatile semantics. Use this instead:

while( *(volatile*) &flag ) ...




当然我打算写的是


while(*(volatile int *)& flag)...



Of course what I meant to write was

while( *(volatile int*) &flag ) ...


2005年6月16日星期四00: 18:03 -0700,google-newsgroups写道:
On Thu, 16 Jun 2005 00:18:03 -0700, google-newsgroups wrote:
你好,

甚至(或因为?)阅读标准(ISO / IEC 9899/1999)我不了解volatile的一些问题。背景是嵌入式编程,在主程序流和中断之间交换数据。

在我工作的组织中,而不是声明变量volatile,
它在必要时被转换为易失性:

示例1(阅读):
int flag; //全局变量,非易失性
while((volatile)标志)//在读取之前强制转换为volatile
;


我假设你的意思是cast(volatile int)标志。这不是你想要的b $ b b。 const和volatile限定符的特殊行为仅适用于左值(尽管类型规则适用于其他情况)。什么

上面的代码是使用非易失性语义读取flag的值

然后将该值转换为volatile int。因为它只是这个

点的值,而不是左值,所以volatile没有效果。这条线是等价的




而(旗帜)


你必须写像


while(*(volatile int *)& flag)


做你想做的事。这里用于读取flag值的* lvalue *具有

类型volatile int。

原因是:当变量声明为volatile时,
编译器根本无法对其进行优化。但是,通过使用强制转换为易失性,可以在某些区域强制执行易失性行为,而其他区域(例如在中断中)仍然可以进行优化。


在一些

区域中使用volatile变量作为非易失性的整个概念听起来很危险。对于不需要易变的部件,你可能最好使用单独的

变量。这可能会使

编译器优化器的工作变得更容易。

另外,即使变量声明不在我的控制范围内,也可以使用它。


这听起来像个奇怪的情况。以这种方式使用的变量应该将
定义为volatile。如果不是这表明定义它的代码

不是为此而设计的,可能不会正常工作。

问题:这是否覆盖按标准?


标准指定不稳定
因为volatile的整个主题非常复杂(例如它与管道相关),有人知道一篇好文章或书这个主题深入讨论了什么?
Hello,

even (or because?) reading the standard (ISO/IEC 9899/1999) I do not
understand some issues with volatile. The background is embedded
programming where data is exchanged between main program flow and
interrupts.

At the organisation I work, instead of declaring a variable volatile,
it is casted to volatile when necessary:

Example 1 (reading):
int flag; // global variable, non-volatile
while( (volatile)flag ) // cast to volatile before reading
;
I assume you mean the cast (volatile int)flag. This doesn''t do what you
want. The special behaviour of the const and volatile qualifiers only
applies to lvalues (although type rules apply in other situations). What
the code above does is read the value of flag using non-volatile semantics
then casts that value to volatile int. Since it 9is just a value at this
point and not an lvalue the volatile has no effect. The line is equivalent
to

while (flag)

You would have to write something like

while (*(volatile int *)&flag)

to do what you want. Here the *lvalue* used to read the value of flag has
type volatile int.
The reason for this is: when the variable is declared volatile, the
compiler cannot optimize it at all. However, by using the cast to
volatile, one can force volatile behavior in some areas, while others
(e.g. in the interrupt) still can be optimized.
The whole concept of using a volatile variable as non-volatile in some
areas sounds dangerous. You would probably be better off using a separate
variable for parts that don''t need to be volatile. That could make the
compiler optimiser''s job easier too.
Addionally, this can be
used even if the variable declaration is not under my control.
It sounds like an odd situation. A variable being used in this way should
be defined as volatile. If it isn''t that suggests the code defining it
isn''t designed for this purpose and probably won''t work properly.
Question: is this covered by the standard?
The standard specifies volatile
Since the whole topic of volatile is very complex (e.g. it is related
to pipeling), does somebody know a good article or book which covers
this topic deeply?




volatile本身与流水线无关。在特定体系结构上实现的易失性是一个机器代码级主题。如果你用C语言编写
,你就让编译器处理细节。


还要考虑sig_atomic_t类型。


劳伦斯



volatile itself has nothing to do with pipelining. How volatile might be
implemented on specific architectures is a machine code level topic. If
you are writing in C you let the compiler handle the details.

Also consider the sig_atomic_t type.

Lawrence


这篇关于变得不稳定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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