volatile and caches question

Andrew Haley aph at redhat.com
Wed Jul 23 18:28:34 UTC 2014

On 23/07/14 14:31, Aleksey Shipilev wrote:
> "Memory flush" has no place since 2005.
> VMs are free to optimize volatile accesses, as long as those
> optimizations fit the Java Memory Model. For example, the multiple
> volatile accesses in constructor can be optimized since we know whether
> the variable is not exposed to other threads. For the variables already
> on heap, it is generally unknown whether we can optimize the accesses
> without breaking the JMM (there are cases where we can do simple optos,
> see Cookbook).
>> > Also if I have multiply threads which operate on
>> > single volatile variable: one writer and many readers and writer doesn't
>> > write too much, will be caches flushed every time readers access volatile
>> > varible and when write didn't write anything? 
> I'm not following what "caches" need "flushing" in this case. Cache
> coherency already takes care about propagating the values.
> Low-level-hardware-speaking, memory semantics around "volatiles" is
> about exposing the data to cache coherency in proper order.

For anyone reading this who still thinks that barriers cause cache
flushes (not pointing at you, Winnie, this is for anyone) I recommend
_Memory Barriers: a Hardware View for Software Hackers_


More information about the hotspot-dev mailing list