class X
{
protected:
volatile DWORD m_vers1;
volatile DWORD m_a;
volatile DWORD m_b;
volatile DWORD m_c;
volatile DWORD m_d;
volatile DWORD m_vers2;
public:
X()
: m_vers1( 0 )
, m_vers2( 0 )
{}
void Write( DWORD a, DWORD b, DWORD c, DWORD d )
{
++m_vers2;
m_a = a;
m_b = b;
m_c = c;
m_d = d;
++m_vers1;
}
void Read( DWORD& ra, DWORD& rb, DWORD& rc, DWORD& rd )
{
for ( ;; )
{
DWORD vers1 = m_vers1;
ra = m_a;
rb = m_b;
rc = m_c;
rd = m_d;
DWORD vers2 = m_vers2;
if ( vers2 == vers1 )
break;
}
}
};Write() and Read() are called from different threads. I'm worried about out-of-order execution principe. Volatile keyword ensures that compiler not change the order. And how to say it to processor? Or this can not worry? How such is made inside critical sections?



