Are volatile variable ‘reads’ as fast as normal reads?

You should really check out this article: http://brooker.co.za/blog/2012/09/10/volatile.html. The blog article argues volatile reads can be a lot slower (also for x86) than non-volatile reads on x86.

  • Test 1 is a parallel read and write to a non-volatile variable. There
    is no visibility mechanism and the results of the reads are
    potentially stale.
  • Test 2 is a parallel read and write to a volatile variable. This does not address the OP’s question specifically. However worth noting that a contended volatile can be very slow.
  • Test 3 is a read to a volatile in a tight loop. Demonstrated is that the semantics of what it means to be volatile indicate that the value can change with each loop iteration. Thus the JVM can not optimize the read and hoist it out of the loop. In Test 1, it is likely the value was read and stored once, thus there is no actual “read” occurring.

Marc Booker's tests

Credit to Marc Booker for running these tests.

Leave a Comment