Re: MSDN volatile sample

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks Ben,


Two more comments,

1.

guarantee updates to global variables. When these are not used, volatile
must be specified to get the memory barriers.

what do you mean memory barrier? Could you provide some other words or more
descriptions please?

2.

I think you mean in the MSDN sample, the change of variable *Sentinel* by
thread2 may not be noticed by thread1. So, thread1 may deadlock? I can
vaguely understand... but if my understanding is correct, could you write
down the code which is the *optimized* code by compiler please (when without
volatile keyword)?


regards,
George

"Ben Voigt [C++ MVP]" wrote:


"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:987FBEA6-277E-43F6-B45D-16FA4E6B4888@xxxxxxxxxxxxxxxx
Thanks Alex,


1.

You are correct. Now I am confused about the MSDN sample. What is it
purpose? Telling us we have to put volatile keyword to thread sharing data
to
make program function well as designed? In my past experience, I have
never
used volatile to all shared data between threads.

Did you use any synchronization primitive, like a critical section or
WaitForSingleObject? If so then these contain a memory barrier which
guarantee updates to global variables. When these are not used, volatile
must be specified to get the memory barriers.


2.

Now I strongly suspect whether compiler will generate any wrong code --
functional wrong code. In MSDN sample, variable Sentinel is used to act as
a
shared variable between thread1 and thread2. Compiler should guarantee
that
both threads can read/write the correct value of Sentinel.

It seems that volatile will make wrong optimization to prevent thread1
from
reading the most recent correct value set by thread2? I think it will
bring
high risks to careless developers, who does not know about volatile and
forget to put it ahead of the variable, which will result in the wrong
optimization of compiler.

Developers who do not know about volatile should not use multiple threads.


What is your perspective on such optimization?


regards,
George

"Alex Blekhman" wrote:

"George" wrote:
Could you provide a link to your quoted Sleep method description
please?

I am reading from,

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

not the same as you quoted.

You're reading in the wrong place. Didn't you noticed that this
page belongs to .NET framework documentation? It has nothing to do
with Win32 API function `Sleep'.

"Sleep"
http://msdn2.microsoft.com/en-us/library/ms686298(VS.85).aspx

Alex






.



Relevant Pages

  • Re: i386 nmi_watchdog: Merge check_nmi_watchdog fixes from x86_64
    ... > after the store without volatile it seems a reasonable ... > as we have taken the address earlier so at some point the compiler ... pointer away and will be referring to it later. ... or if the compiler does whole-program optimization and can see ...
    (Linux-Kernel)
  • Re: Is there a way to flush registers and tell the C compiler to refill?
    ... Because using volatile would prevent some C compilation optimization, ... is nothing to worry about (other than a broken compiler). ... Such a caching ...
    (comp.lang.c)
  • Re: [PATCH] "volatile considered harmful" document
    ... "Volatile is often used to prevent optimization, ... +safe (spinlocks, mutexes, memory barriers, etc.) are designed to prevent ... +primitives act as memory barriers - they are explicitly written to do so - ...
    (Linux-Kernel)
  • Re: ifort volatile with O3
    ... move code out of loops when possible. ... such that the VOLATILE parts don't do what ... the OP did ask about optimization level 3. ... compiler is free to move things around so long as the final result ...
    (comp.lang.fortran)
  • Re: [PATCH] "volatile considered harmful" document
    ... Just tell the reasons why volatile is bad and what should be used instead, ... "Volatile is often used to prevent optimization, ... Access to data must be protected and handled with kernel provided synchronization, ... +primitives act as memory barriers - they are explicitly written to do so - ...
    (Linux-Kernel)