title: volatile and mutex for inter thread communication
body: <p>I have the following class</p>
  
  <pre><code>class Thread
  {
     int m_state;
     Mutex m;
     CondVar v;
  
     Thread(): m_state( 0 ) {}
  
     write()
     {
       // Get mutex m
       // m_state = 1;
       // signal v
     };
  
     read()
     {
        // Get mutex m
        while( m_state == 0 )
        {
           wait v;
        };
     };
  };
  </code></pre>
  
  <p>read() and write() can be called on separate threads.</p>
  
  <p>In this case, do we have to make m_state or volatile or the compiler ensures that optimizations ( such as reading m_state from memory to register and spinning on register ) is prevented ?</p>