English 中文(简体)
是否利用“点人到不稳定”来防止汇编者在任何时候都达到最佳程度?
原标题:Does using "pointer to volatile" prevent compiler optimizations at all times?
  • 时间:2010-09-24 09:34:37
  •  标签:
  • c++
  • volatile
最佳回答

编辑可在because buffer<>/code>上优化你的代码。 这不是一个挥发性物体

《标准》只要求汇编者严格遵守挥发性物体的特性。 这里指的是C++03。

符合执行要求的要求最少:

  • At sequence points, volatile objects are stable in the sense that previous evaluations are complete and subsequent evaluations have not yet occurred. [...]

以及

The observable behavior of the abstract machine is its sequence of reads 以及writes to volatile data and calls to library I/O functions

In your example, what you have are reads 以及writes using volatile lvalues to non-volatile objects. C++0x removed the second text I quoted above, because it s redundant. C++0x just says

符合执行要求的要求最少:

  • Access to volatile objects are evaluated strictly according to the rules of the abstract machine.[...]

这些方案统称为值得注意的行为。

While one may argue that "volatile data" could maybe mean "data accessed by volatile lvalues", which would still be quite a stretch, the C++0x wording removed all doubts about your code 以及clearly allows implementations to optimize it away.

But as people pointed out to me, It probably does not matter in practice. A compiler that optimizes such a thing will most probably go against the programmers intention (why would someone have a pointer to volatile otherwise) 以及so would probably contain a bug. Still, I have experienced compiler vendors that cited these paragraphs when they were faced with bugreports about their over-aggressive optimizations. In the end, volatile is inherent platform specific 以及you are supposed to double check the result anyway.

问题回答

从最后的C++0x草案[生效]:

8 The least requirements on a conforming implementation are:

— Access to volatile objects are evaluated strictly according to the rules of the abstract machine.

[......]

12 Accessing an object designated by a volatile glvalue (3.10), modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects, [......]

因此,即使你提供的法典也不得优化。

您希望删除的记忆内容可能已经从贵邮联/核心内藏中流出到援助团,在那里其他邮联能够继续看到。 在撰写文章后,你需要使用双向/记忆屏障指示/原子操作或某种东西,以引发与其他核心的结合。 在实践中,贵编者在要求任何外部职能之前,或许会这样做(戈格尔·达维·布滕金在多读过程中挥发的可疑用途上担任职务),这样,如果你在不久之后准备好,不会成为一个重大问题。 简言之,需要挥发性。

符合要求的执行在休闲时,可推迟任何挥发性的读写字的实际表现,直至变幻莫测的读写结果会影响执行挥发性书写或I/O操作。

例如:

volatile unsigned char vol1,vol2;
extern unsigned char res[1000];
void test(int scale)
{
  unsigned char ch;

  for (int 0=0; i<10000; i++)
  {
    res[i] = i*vol1*scale;
    vol2 = res[i];
  }
}

a 符合规格的汇编者可在其选择中核对<代码>> > > 比例 是128的多种,如果是,在从<代码>vol1做任何读物之前,从<代码>中删除。 页: 1 即使汇编者需要从<代码>vol1上读出。 在编汇到<代码>第2卷<>>之前,编汇人可推迟两个业务,直至其操作基本不受限制的代码。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签