[concurrency-interest] Question about reordering and volatile

Peter Veentjer alarmnummer at gmail.com
Tue Apr 1 08:04:59 EDT 2008

I have a question about reorderings and volatile writes.

Reorderings and volatile writes follow the 'roach motel'  (just like
synchronized blocks) that prevents instructions from jumping over.

Allowing an instruction before a volatile write on variable x to jump
over that write, can lead to not seeing the effects of that
instruction when you do the volatile read on x.This is not allowed
according to the JMM: all changes made in thread a when it does a
volatile write on  x, are visible when thread b does a volatile read
on x. Ok.. no problem so far.

But what about instructions after the volatile write? If these were
allowed (and they are not according to the roach motel principle) to
jump in front of the volatile write of x, thread b could see these
changes as well. The results are exactly the same as the situation
that the read of x (by thread b) happens after the instruction after
the volatile write has executed (by thread a). So the letting the
instruction after the write to (volatile variable) x, in front of this
write, would not cause any havoc.

So what is the reason that instructions after the volatile write are
not allowed to jump over this volatile write?

More information about the Concurrency-interest mailing list