MSDN volatile sample



 DEVELOP > c-Plus-Plus > MSDN volatile sample

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "George2"
Date: 28 Dec 2007 02:53:28 AM
Object: MSDN volatile sample
Hello everyone,
In the MSDN volatile sample,
http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx
I do not understand what is the purpose of the sample. I have tried to
remove the keyword volatile, and the result is the same. :-)
Any ideas?
thanks in advance,
George
.

User: "Michal Nazarewicz"

Title: Re: MSDN volatile sample 07 Jan 2008 01:04:19 PM
George2 <george4academic@yahoo.com> writes:

In the MSDN volatile sample,

http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx

I do not understand what is the purpose of the sample. I have tried to
remove the keyword volatile, and the result is the same. :-)

Any ideas?

volatile means that given variable may change at any time and compiler
have no way of knowing that it may happen (no, it's not the definition
of volatile keyword but it helps understand it). Assume that compiler
knows that Sleep() function will never modify Sentinel -- in this case
a loop
while (Sentinel)
Sleep(0);
might be replaced with
if (Sentinel)
while (1)
Sleep(0);
if there would be no volatile keyword. That's because without volatile
keyword compiler could came to a conclusion that value of Sentinel does
not change in body of the loop so if the condition was true at the
beginning it will be true all the time.
With volatile keyword compiler must check Sentinel's value since it no
longer knows that it's value was not changed (that's basically meaning
of that keyword).
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
.
User: "Joe Greer"

Title: Re: MSDN volatile sample 07 Jan 2008 01:15:20 PM
Michal Nazarewicz <mina86@tlen.pl> wrote in
news:87hchpjw3w.fsf@erwin.mina86.com:

George2 <george4academic@yahoo.com> writes:

In the MSDN volatile sample,

http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx

I do not understand what is the purpose of the sample. I have tried

to

remove the keyword volatile, and the result is the same. :-)

Any ideas?


volatile means that given variable may change at any time and compiler
have no way of knowing that it may happen (no, it's not the definition
of volatile keyword but it helps understand it). Assume that compiler
knows that Sleep() function will never modify Sentinel -- in this case
a loop

while (Sentinel)
Sleep(0);

might be replaced with

if (Sentinel)
while (1)
Sleep(0);

if there would be no volatile keyword. That's because without

volatile

keyword compiler could came to a conclusion that value of Sentinel

does

not change in body of the loop so if the condition was true at the
beginning it will be true all the time.

With volatile keyword compiler must check Sentinel's value since it no
longer knows that it's value was not changed (that's basically meaning
of that keyword).

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>
Hope that helps,
joe
.
User: "James Kanze"

Title: Re: MSDN volatile sample 08 Jan 2008 07:11:48 AM
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
.
User: "Chris Thomasson"

Title: Re: MSDN volatile sample 08 Jan 2008 07:34:54 AM
"James Kanze" <james.kanze@gmail.com> wrote in message
news:4d6b6d00-a680-4f51-b77d-11ebcff3ed43@e23g2000prf.googlegroups.com...
[...]

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.

This might be useful:
http://developer.intel.com/products/processor/manuals/318147.pdf
?
[...]
.
User: "Chris Thomasson"

Title: Re: MSDN volatile sample 08 Jan 2008 07:38:51 AM
"Chris Thomasson" <cristom@comcast.net> wrote in message
news:p-OdnUA7d_ty5B7anZ2dnUVZ_tijnZ2d@comcast.com...

"James Kanze" <james.kanze@gmail.com> wrote in message
news:4d6b6d00-a680-4f51-b77d-11ebcff3ed43@e23g2000prf.googlegroups.com...
[...]

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.


This might be useful:

http://developer.intel.com/products/processor/manuals/318147.pdf

?

AFAICT, they only need extra memory ordering instructions on XBOX and
Itanic...
.





User: "Dave Rahardja"

Title: Re: MSDN volatile sample 28 Dec 2007 06:32:30 PM
On 2007-12-28 02:53:28 -0600, George2 <george4academic@yahoo.com> said:

Hello everyone,


In the MSDN volatile sample,

http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx

Slightly off topic...
Note that the use of volatile for multithreading on today's
architectures is problematic. Coherency issues, especially on
pipelined, cached, or multi-core systems (which is most of today's
PCs), can become complicated, and is not completely addressed by the
use of volatile.
....and back to C++.
Since the C++ standard does not even recognize the concept of
multithreading, it is much better to rely on OS facilities to perform
interthread synchronization.
....and off again.
Besides, spinlocks are a no-no in my book unless it's in a kernel-mode
device driver that's gated by hardware with very low latency.
-dr
.

User: "Dave Rahardja"

Title: Re: MSDN volatile sample 28 Dec 2007 06:21:08 PM
On 2007-12-28 02:53:28 -0600, George2 <george4academic@yahoo.com> said:

http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx

Compile it with optimizations on (i.e. Release build).
-dr
.

User: ""

Title: Re: MSDN volatile sample 28 Dec 2007 06:56:26 PM
On Dec 28, 12:53=A0am, George2 <george4acade...@yahoo.com> wrote:

Hello everyone,

In the MSDN volatile sample,

http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx

I do not understand what is the purpose of the sample. I have tried to
remove the keyword volatile, and the result is the same. :-)

Any ideas?

thanks in advance,
George

I would not rely on volatile for anything related to threads. Think of
it as more of a hint to the compiler when it's considering the
variable's scope (especially when optimizing).
.
User: "Dave Rahardja"

Title: Re: MSDN volatile sample 28 Dec 2007 10:34:51 PM
On 2007-12-28 18:56:26 -0600,
said:

On Dec 28, 12:53 am, George2 <george4acade...@yahoo.com> wrote:

Hello everyone,

In the MSDN volatile sample,

http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx

I do not understand what is the purpose of the sample. I have tried to
remove the keyword volatile, and the result is the same. :-)

Any ideas?

thanks in advance,
George


I would not rely on volatile for anything related to threads. Think of
it as more of a hint to the compiler when it's considering the
variable's scope (especially when optimizing).

I don't understand what you mean. volatile has nothing to do with scope
and everything to do with optimization.
Beyond its sequence-point properties, the behavior of volatile
variables is completely implementation-defined, which means that the
effect of the volatile qualifier is not portable in any meaningful,
externally-observable way.
Thus, I wouldn't use it for anything portable, period.
-dr
.
User: "spaglia"

Title: Re: MSDN volatile sample 29 Dec 2007 11:13:10 AM
On Dec 28, 8:34=A0pm, Dave Rahardja
<drahardja.place...@sign.here.pobox.com> wrote:

On 2007-12-28 18:56:26 -0600,

said:





On Dec 28, 12:53=A0am, George2 <george4acade...@yahoo.com> wrote:

Hello everyone,


In the MSDN volatile sample,


http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx


I do not understand what is the purpose of the sample. I have tried to
remove the keyword volatile, and the result is the same. :-)


Any ideas?


thanks in advance,
George


I would not rely on volatile for anything related to threads. Think of
it as more of a hint to the compiler when it's considering the
variable's scope (especially when optimizing).


I don't understand what you mean. volatile has nothing to do with scope
and everything to do with optimization.

Beyond its sequence-point properties, the behavior of volatile
variables is completely implementation-defined, which means that the
effect of the volatile qualifier is not portable in any meaningful,
externally-observable way.

Thus, I wouldn't use it for anything portable, period.

-dr- Hide quoted text -

- Show quoted text -

I believe we are in agreement. My use of scope was targeted to the
optimizer and not the language proper.
-Steve
.



User: "Chris Thomasson"

Title: Re: MSDN volatile sample 07 Jan 2008 06:16:54 AM
[included comp.programming.threads]
"George2" <george4academic@yahoo.com> wrote in message
news:ddca66ef-df4b-49d8-8d30-e04c6351c3e4@t1g2000pra.googlegroups.com...

Hello everyone,


In the MSDN volatile sample,

http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx

I do not understand what is the purpose of the sample. I have tried to
remove the keyword volatile, and the result is the same. :-)

Any ideas?

It means that MS compilers for PowerPC (e.g., XBOX), and Itanium (e.g.,
Itanic) based platforms do indeed assign significantly __overbearing!__
memory-barrier functionality to their most "basic" meaning of the volatile
keyword itself; by default! ;^(... Volatile is implementation defined
INDEED!
Basically, they assign heavy acquire-load membar semantics to EVERY _naked_
load, and release-store semantics to EVERY _naked_ store; this could be due
to their relationship with Intel I suppose. Keep in mind that Java does this
as well for programming "simplicity"; IMVHO, all of that crap is MAJOR
overkill and generates unneeded overheads on the memory coherency system in
general. Dumbs down things to a level that makes things ridiculous.
They certainly do NOT have any real need to define volatile such that it has
anything to do with shared-memory threading. IMO, using volatile and
threading in then same sentence is certainly misleading to say the least,
and usually winds up getting people to make erroneous assumptions about
C/C++ compilers...
;^(...
.
User: "James Kanze"

Title: Re: MSDN volatile sample 07 Jan 2008 10:07:04 AM
On Jan 7, 1:16 pm, "Chris Thomasson" <cris...@comcast.net> wrote:

[included comp.programming.threads]
"George2" <george4acade...@yahoo.com> wrote in message
news:ddca66ef-df4b-49d8-8d30-e04c6351c3e4@t1g2000pra.googlegroups.com...

Hello everyone,
In the MSDN volatile sample,
http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx
I do not understand what is the purpose of the sample. I
have tried to remove the keyword volatile, and the result is
the same. :-)

Maybe. Sometimes.
Without the volatile, the result is undefined behavior.
Depending on the compiler and the processor, it might or it
might not work---changing the optimization level, or running on
a multi-core rather than a single core, make cause the behavior
to be different.
With the volatile, the result is implementation defined
behavior. A compiler may (but is not required to) define some
useful semantics for this case. Most compilers don't, at least
where multi-core systems are concerned. (I don't think that the
code in question will behave reliably on a multi-core system
even when compiled with VC++, at least through version 8.)

Any ideas?

It means that MS compilers for PowerPC (e.g., XBOX), and
Itanium (e.g., Itanic) based platforms do indeed assign
significantly __overbearing!__ memory-barrier functionality to
their most "basic" meaning of the volatile keyword itself; by
default! ;^(... Volatile is implementation defined INDEED!
Basically, they assign heavy acquire-load membar semantics to
EVERY _naked_ load, and release-store semantics to EVERY
_naked_ store; this could be due to their relationship with
Intel I suppose. Keep in mind that Java does this as well for
programming "simplicity"; IMVHO, all of that crap is MAJOR
overkill and generates unneeded overheads on the memory
coherency system in general. Dumbs down things to a level that
makes things ridiculous.

I'm not really sure whether the intent of the example is to show
that (VC++ 8 didn't assign those semantics to volatile, at least
not on Intel architectures), or whether it isn't simple another
case of an author not knowing what he is writing about. I've
seen a lot of authors suggest something similar, with the
assumption that it was more or less portable, accross all
compilers.
I might add that a compiler which gives volatile such heavy
semantics doesn't bother me that much, since I never really use
volatile. On a Sparc, as far as I know, there is no way of
synchronizing memory access other than by using a membar
instruction---if volatile is to be used for memory mapped I/O
(it's original rationale), then the compiler must generate the
acquire-load or release-store member instructions.

They certainly do NOT have any real need to define volatile
such that it has anything to do with shared-memory threading.
IMO, using volatile and threading in then same sentence is
certainly misleading to say the least, and usually winds up
getting people to make erroneous assumptions about C/C++
compilers...

Very much agreed. Volatile was really designed for things that
don't behave like memory. Other mechanisms are necessary (and
being proposed) for threading.
I might add that the quality of the MSDN pages is very variable.
I often get the impression that they are written by someone who
doesn't really understand all of the technical issues---they
frequently neglect to give essential information, for example.
(Microsoft is not alone in this: a lot of documentation is
written by technical writers---with the accent on "writers".)
--
James Kanze (GABI Software) mailto:james.kanze@gmail.com
Conseils en informatique orient=EF=BF=BDe objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=EF=BF=BDmard, 78210 St.-Cyr-l'=EF=BF=BDcole, France, +33 (0)1 30 2=
3 00 34
.
User: "Chris Thomasson"

Title: Re: MSDN volatile sample 07 Jan 2008 08:07:18 PM
"James Kanze" <james.kanze@gmail.com> wrote in message
news:0d2ba4a2-742d-418b-bbb1-76f992283477@v29g2000hsf.googlegroups.com...

On Jan 7, 1:16 pm, "Chris Thomasson" <cris...@comcast.net> wrote:

[included comp.programming.threads]

Whoops, forgot to include cpt... Well, we can keep this here.
[...]

It means that MS compilers for PowerPC (e.g., XBOX), and
Itanium (e.g., Itanic) based platforms do indeed assign
significantly __overbearing!__ memory-barrier functionality to
their most "basic" meaning of the volatile keyword itself; by
default! ;^(... Volatile is implementation defined INDEED!

[...]

I'm not really sure whether the intent of the example is to show
that

Well, there is a race-condition if they don't inject the correct barriers.
They are trying to show an example of a producer/consumer pattern such that
the thread which mutates the 'Sentinel' variable is the producer, and the
thread that loads from 'Sentinel' is the consumer. The load needs at least a
#LoadLoad barrier after it, and the store needs at least a #StoreStore
before it. If those barriers are omitted, then the consumer can observe the
store made from the producer _before_ the mutation to the 'CriticalData'
variable is rendered visible. That would be bad...
[...]

They certainly do NOT have any real need to define volatile
such that it has anything to do with shared-memory threading.
IMO, using volatile and threading in then same sentence is
certainly misleading to say the least, and usually winds up
getting people to make erroneous assumptions about C/C++
compilers...

Very much agreed. Volatile was really designed for things that
don't behave like memory. Other mechanisms are necessary (and
being proposed) for threading.
I might add that the quality of the MSDN pages is very variable.
I often get the impression that they are written by someone who
doesn't really understand all of the technical issues---they
frequently neglect to give essential information, for example.
(Microsoft is not alone in this: a lot of documentation is
written by technical writers---with the accent on "writers".)

I think your absolutely correct:
http://groups.google.com/group/comp.programming.threads/browse_frm/thread/1b3d65e90d3f5e24
:^0
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER