Re: MSDN volatile sample



Thanks Norbert,


while (Sentinel)
Sleep(0); // volatile spin lock

I think even if the original code will result in deadlock. In the original
MSDN sample, Sleep(0) will sleep forever until there is a signal sent to the
sleeping thread. But in the original sample, there is no way to trigger the
thread from waking up.

The sample works by chance because Sentinel happened to false when we enters
whle loop.

Any comments? If I am wrong, please feel free to correct me. :-)


regards,
George

"Norbert Unterberg" wrote:


George schrieb:
Thanks Norbert,


What is the possible different results when we removed the volatile (even if
I can not find any different result on my machine) of the MSDN sample
compared with the original case when we add the volatile keyword?


The compiler could notice that in the loop in ThreadFunc1 the variable Sentinel
is never changed, so it could optimize the loop from

while (Sentinel)
Sleep(0); // volatile spin lock

to something like

if (Sentinel)
for (;;)
Sleep(0);

and the loop would never end.
The volatile keyword forces the compiler to evaulate the Sentinel variable in
every loop so setting Sentinel to true in ThreadFunc2 won't remain undetected.





regards,
George

"Norbert Unterberg" wrote:

George schrieb:

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. :-)
THe result may be the same with your compiler version and optimizer setting, but
is is not guaranteed to work without volatile. volatile tells the compiler not
to optimize the use of the variable because it could be modified at any time by
something the compiler has no control over.

Code with a "forgotten volatile" at the right place can work for years but fail
unexpected after minor unrelated code changes.

Norbert


.



Relevant Pages

  • Re: [patch] spinlocks: remove volatile
    ... and somebody added "volatile" to hide the problem). ... The 'C' compiler has no choice but to actually make that memory access ... 'knows' that spinner is FALSE, ... The compiler can only optimize away that loop if spinner is provably const. ...
    (Linux-Kernel)
  • Re: MSDN volatile sample
    ... The compiler is not allowed to optimize Sentinel check out of the loop, ... volatile or not. ... Now I am confused about the MSDN sample. ...
    (microsoft.public.vc.language)
  • Re: why are some atomic_ts not volatile, while most are?
    ... intelligent optimiser could notice that the address of v doesn't change in the loop and the destination is never written within the loop, so the read could be hoisted out of the loop. ... In the "volatile considered evil" discussion in May of this year, Alan Cox explicitly mentioned the implementation of atomic primitives as a case where "volatile" might be required. ... When the compiler detects data dependencies within a thread of execution, it will do the right thing. ... putting the burden on the compiler to enforce serialization. ...
    (Linux-Kernel)
  • Re: Proper Use of Volatile?
    ... I'm not quite sure when I should use volatile. ... extern char volatile StatusRegister; ... If StatusRegister weren't "volatile", then the compiler could ... exit the loop immediately, or turn it into an infinite loop. ...
    (comp.lang.c)
  • Re: How to manipulate a receiving buffer
    ... interrupt handler only changes head, and the main code only changes ... 'volatile' tells the compiler not to optimize away memory references. ... while loop, ...
    (comp.arch.embedded)