If I understand meaning of volatile and MemoryBarrier correctly than the program below has never to be able to show any result.
It catches reordering of write operations
I don't think this is re-ordering.
This piece of code is simply not thread-safe:
while (continueChecking)
{
int tempA = a;
int tempB = b;
...
I think this scenario is possible:
int tempA = a;
executes with the values of the last loop (a == 2)b = 10
and the loop stopsint tempB = b;
executes with b == 10I notice that the calls to MemoryBarrier() enhance the chances of this scenario. Probably because they cause more context-switching.