Re: Newbie question: accessing global variable on multiprocessor



On Feb 6, 9:02 am, "Boris" <spa...@xxxxxxxxxx> wrote:

That doesn't help you. It doesn't provide specific, guaranteed
semantics. Declaring that the object can be modified by another thread
doesn't mean that doing so without locks or Interlocked* operations
has guaranteed semantics.

Basically, what I mean: 'volatile' can also be used for other purposes (than
multi-processing). For example, DMA transfer from some device (into memory).
I just thought that because of this semantics (of 'volatile' ),
implementations make sure that memory values are always used (as opposed to
cached values). I don't know if it's true or not, of course. Just wondering.

If you know for a fact that compiler optimizations are your only
issue, then 'volatile' may well do what you need. It is, however,
impossible to know this in general, since any optimization the
compiler can make can, at least in theory, be made by the CPU.

In practice, you may know what optimizations the hardware and the
compiler are capable of making. But relying on that knowledge tends to
produce very fragile code that fails when a new CPU comes out, when
it's compiled with a smarter processor, or that is very difficult to
port.

With the exception of the "terminate flag" application and those
defined by the C/C++ standards, use of 'volatile' should be considered
only as part of platform-specific optimizations.

DS
.



Relevant Pages

  • Re: WaitForSingleObject() will not deadlock
    ... One is to hijack the semantics of volatile to disable compiler optimizations ... and otherwise let the compiler to agressive optimization. ... Agressive optimizations are the ones that work on the edge of the semantics of the ... Because the compiler can see into lock and unlock, it is able to reduce f ...
    (microsoft.public.vc.mfc)
  • Re: WaitForSingleObject() will not deadlock
    ... represent an incorrect implementation of the language. ... the *compiler* does not guarantee this. ... but to state it in terms of the execution instead of the formal semantics of the language ... as long as the optimizations do not change the semantics of the language). ...
    (microsoft.public.vc.mfc)
  • Re: Substitute for volatile
    ... > I am a newbie w.r.t. compiler optimizations and I believe these ... "volatile" is a hint to the compiler that the contents of a variable may ... optimizations are at a function level, ...
    (comp.lang.c)
  • Re: Volatile + multithreading
    ... The volatile keyword has little use in multithreaded programming. ... > field or global seems to have no impact because it seems the compiler will ... operations as "special" and suppress optimizations around them. ... > majority of the multithreading issues that could have resulted from ...
    (microsoft.public.vc.language)
  • Re: [OT] volatile in userspace
    ... Without 'volatile' and disabling optimizations altogether, ... 'Compiler over-optimisations and "volatile"'), volatile is used to ... Well, if you look at the Wiki, it admits that this is a bug: ...
    (Linux-Kernel)