Objective: I have a chunk of memory shared between several x86/x64 processes/threads. Inside it I have QWORD (64 bit) variable aligned on 8-byte boundary. That variable is accessed by one writer and several readers simultaneously, mix of x86 and x64 readers/writers is possible. Let's say that variable is declared/named as QWORD qwSharedVar, and compiler is MSVC with /volatile:ms behaviour.
Suppositions:
1. If writer process is x64, it writes a new value in a such way:
*reinterpret_cast< volatile QWORD* >( &qwSharedVar ) = qwNewValue;
If writer process is x86, it writes a new value in a such way:
_InterlockedCompareExchange64( reinterpret_cast< volatile __int64* >( &qwSharedVar ), qwNewValue, qwSharedVar );
If reader process is x64, it reads a shared value in a such way:
QWORD qwValueNow = *reinterpret_cast< volatile QWORD* >( &qwSharedVar );
If reader process is x86, it reads a shared value in a such way:
QWORD qwValueNow = _InterlockedCompareExchange64( reinterpret_cast< volatile __int64* >( &qwSharedVar ), 0, 0 );
If reader process is x86 or x64, and it wants to read only lower DWORD of shared value, then:
DWORD dwValueNow = *reinterpret_cast< volatile DWORD* >( &qwSharedVar );
Are those suppositions legal? I'm especially worried about point 5, where mix of accesses through "lock cmpxchg8b" and "mov dword ptr" is possible.