I'm not sure I completely agree with the other answers here; biziclop's answer is right as far as it goes, but I'm not sure we can conclude that you're safe unless we know more detail.
In the simple case, interleaving might look like this:
Thread 1 (writer) Thread 2 (Writer) Thread 3 (Reader)
----------------- ----------------- -----------------
flag = true;
if (flag) {
flag = true;
flag = false;
doStuff();
and this might be fine (the second set of flag
to true
doesn't matter, as doStuff()
will still presumably see whatever Thread 2 needs doing.
However if you reverse the order thread 3 does:
Thread 1 (writer) Thread 2 (Writer) Thread 3 (Reader)
----------------- ----------------- -----------------
flag = true;
if (flag) {
doStuff();
flag = true;
flag = false;
then Thread 2's update could be lost.
Of course you need to be similarly careful with whatever else Thread 2 does, to make sure it's visible to Thread 3. If there's other state that Thread 2 needs to set, order becomes important there too.
In the simple case, yes you're fine, but if it gets more complex than just simple flicking of flags then this becomes much harder to reason about.