Re: MSDN volatile sample



Thanks Alex,


On the contrary, declaring `Sentinel' variable as volatile will
prevent optimizations and force a compiler to generate the code
that reads `Sentinel' value everty time it's accessed.

Could you write down the code which is the *optimized* code by compiler
please (when without volatile keyword)? I think you mean the optimized code
will make thread1 deadlock without read the actual updated value of variable
Sentinel. But I can not imagine how compiler optimized code looks like. :-)


regards,
George

"Alex Blekhman" wrote:

"George" wrote:
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.

It depends on how you access your shared data. In this particular
sample volatile variable is accessed from different threads.
Without `volatile' modifier compiler may optimize away reads of
this variable. So, the code in a thread may miss variable changes.
To prevent optimizations, you specify a variable as volatile.
Norbert already answered this question.

It seems that volatile will make wrong optimization to prevent
thread1 from
reading the most recent correct value set by thread2?

On the contrary, declaring `Sentinel' variable as volatile will
prevent optimizations and force a compiler to generate the code
that reads `Sentinel' value everty time it's accessed.

Alex



.



Relevant Pages

  • 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)
  • Re: Share .cpp and .h along projects
    ... larger object can be controlled in a threadsafe manner using a volatile ... pointer and memory barriers. ... The compiler DOES apply those optimizations. ...
    (microsoft.public.vc.language)
  • Re: multiple threads and volatile
    ... if a variable is cached in a register, no memory barrier is going to make it ... not have a single 'volatile' among them. ... static and the compiler can prove that its address is not exported to other ... and that thread1 does not modify x on its own. ...
    (comp.programming.threads)

Quantcast