I have the following class
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;
};
};
};
read() and write() can be called on separate threads.
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 ?