Re: MSDN volatile sample

Tech-Archive recommends: Speed Up your PC by fixing your registry




"Bo Persson" <bop@xxxxxx> wrote in message
news:5tv4lqF1fjbiiU1@xxxxxxxxxxxxxxxxxxxxx
George wrote:
:: Yes, I agree Bo!
::
::
:: We are a little off-topic now. :-)
::
:: I have made some self-study and analysis. I think volatile has
:: nothing to do with thread synchronization, i.e. if we want to
:: share some data between threads, we should use thread
:: synchronization approach, like mutex other than volatile --
:: volatile is not one of the thread synchronization approaches.
::
:: 1. mutex (and other synchronization approach) is used to control
:: thread synchronization;
::
:: 2. volatile is used to prevent compiler from optimization
::
:: (1) and (2) are totally two different things and should not be
:: mixed.
::
:: My confusion is, whether we should add both mutex and volatile to
:: the shared data? Because even if mutex could make thread shared
:: data in a safe synchronized way, we are not 100% sure whether at
:: compiler level, it will make some wrong optimization -- so using
:: volatile at the same time is safe?

Using both of them is unneccessary. Except when there is a special
compiler extension (like VC8, VC9) , volatile is not enough for thread
synchronization.

When you use something like a mutex, that also disables some reordering of
reads and writes, because either
- the compiler knows about mutexes, and behaves correctly, or
- the mutex is an OS level operation, so the compiler doesn't know enough
to optimize

I think this isn't right, because aliasing analysis may permit a conforming
compiler to optimize access to private member variables even in the presence
of arbitrary function calls, if the address of the variable is never taken.
The same holds true for file-scoped variables (C static globals or C++ anon
namespace).



Bo Persson




.



Relevant Pages

  • Re: MSDN volatile sample
    ... threads, we should use thread synchronization approach, like mutex other than ... volatile -- volatile is not one of the thread synchronization approaches. ... synchronized way, we are not 100% sure whether at compiler level, it will ... make some wrong optimization -- so using volatile at the same time is safe? ...
    (microsoft.public.vc.language)
  • Re: MSDN volatile sample
    ... :: volatile is not one of the thread synchronization approaches. ... volatile is used to prevent compiler from optimization ... whether we should add both mutex and volatile to ...
    (microsoft.public.vc.language)
  • Re: shared memory between processes
    ... > compiler to always refer to the actual memory location are neccesary. ... need volatile. ... no mutex and yes, ...
    (comp.os.linux.development.system)
  • Re: MSDN volatile sample
    ... volatile just slows down your code and confounds ... Conversely, if you use volatile instead of a mutex, your code is probably ... depending on the compiler and the machine architecture, ... anything atomic that isn't already atomic, and atomicity is frequently ...
    (microsoft.public.vc.language)
  • Re: MSDN volatile sample
    ... :: I am wondering if you use mutex or other technologies, ... The general use for volatile is in interfacing with external hardware. ... The volatile keyword tells the compiler that each read from ... MSDN describes Microsoft's implementation of C++, ...
    (microsoft.public.vc.language)