Should I always care about atomicity when assigning values in multi-threaded projects? I have two threads running parallel. Can I safely change non-DWORD variable if it is used as flag only? Or do I have to use DWORD aligned variable (or DWORD itself) since Microsoft guarantees that it will be changed atomically? Or do I have to slow down my code and use Interlocked*() functions instead? Will my code still work fine if I go down from 32- to 16-bits system or go up from 32- to 64-bits system?
/* real value doesn't matter, only null or not-null */
short flag;
// DWORD flag;
DWORD WINAPI thread_1(LPVOID* param)
{
while(true){/* do stuff, flag can be changed non-atomically */}
return 0;
}
DWORD WINAPI thread_2(LPVOID* param)
{
while(true){if(flag){/* do stuff */}}
return 0;
}
update
thread_2
is only observing the flag while thread_1
changes it.