Re: MSDN volatile sample
On Jan 7, 8:15 pm, Joe Greer <jgr...@doubletake.com> wrote:
Michal Nazarewicz <min...@tlen.pl> wrote
innews:87hchpjw3w.fsf@erwin.mina86.com:
[...]
In addition to the above, Microsoft added additional semantics to the
volatile keyword. From their msdn site:
<begin quote>
Microsoft Specific
Objects declared as volatile are not used in certain
optimizations because their values can change at any time. The
system always reads the current value of a volatile object at
the point it is requested, even if a previous instruction
asked for a value from the same object. Also, the value of the
object is written immediately on assignment.
Also, when optimizing, the compiler must maintain ordering
among references to volatile objects as well as references to
other global objects. In particular,
A write to a volatile object (volatile write) has Release
semantics; a reference to a global or static object that
occurs before a write to a volatile object in the instruction
sequence will occur before that volatile write in the compiled
binary.
A read of a volatile object (volatile read) has Acquire
semantics; a reference to a global or static object that
occurs after a read of volatile memory in the instruction
sequence will occur after that volatile read in the compiled
binary.
This allows volatile objects to be used for memory locks and
releases in multithreaded applications.
<end quote>
Interesting. The problem is that unless I've misunderstood
something about the x86 architecture, this description doesn't
correspond to the code generated by VC++ 8. I'm pretty sure
that the achieve these semantics, you need some special code: at
the least, a lock prefix on the accesses to the volatile.
Looking at the generated code, I don't see one.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34