I have a volatile bool 'play' flag in my class that is being set by one thread and being read by another thread.
Do I need to synchronize the calls to that flag? for example in this function:
void stop()
{
play = false;
}
In windows they have the _InterlockedExchange and OSX has OSAtomicAdd64Barrier, I've seen those functions being used with shared primitives, do I need them?
Thank you
Depends:
If you are on a CPU that has total store order memory model (eg x86 / x64) or any machine with only 1 CPU core, then the answer to your question is no given that you state that only 1 thread writes to the flags, and barrier directives are probably optimized away anyway on x86. If you compile the same code on a CPU that has a relaxed memory model, then the situation changes, and you may then find code that works perfectly on an x86 develops some bizarre and difficult to reproduce bugs if you compile and run it on an ARM or PPC for example
The volatile
directive prevents the write being cached somewhere, and may mean that the reading thread sees the write much sooner than it would in the volatile
directive weren't there. Whether you want to use this depends on how important this interval is