Re: MSDN volatile sample
- From: George <George@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 28 Dec 2007 21:52:00 -0800
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
- Follow-Ups:
- Re: MSDN volatile sample
- From: Alex Blekhman
- Re: MSDN volatile sample
- References:
- Re: MSDN volatile sample
- From: Norbert Unterberg
- Re: MSDN volatile sample
- From: Norbert Unterberg
- Re: MSDN volatile sample
- From: George
- Re: MSDN volatile sample
- From: ajk
- Re: MSDN volatile sample
- From: George
- Re: MSDN volatile sample
- From: Alex Blekhman
- Re: MSDN volatile sample
- From: George
- Re: MSDN volatile sample
- From: Alex Blekhman
- Re: MSDN volatile sample
- Prev by Date: Re: MSDN volatile sample
- Next by Date: Re: MSDN volatile sample
- Previous by thread: Re: MSDN volatile sample
- Next by thread: Re: MSDN volatile sample
- Index(es):
Relevant Pages
|