Re: JavaMemoryModel: Question about the semantics of volatile

From: Bill Pugh (pugh@cs.umd.edu)
Date: Wed Mar 17 2004 - 20:24:00 EST


OK, I'd admit that my personal preference is for the strong
interpretation.

But in the two most common uses of volatiles, it won't make a
difference.

In one case, as you would see in ConcurrentHashMap, all threads that
are going
to write to the volatile will be serialized by holding a regular lock
while the volatile
is being updated. In this case, each thread that grabs the lock will be
synchronized
with all previous threads that updated the volatile, so the weak vs.
strong interpretation
don't matter.

In the other common case, a volatile field is used to store a reference
to an object. Anyone
who reads that volatile field and gets a particular reference only
cares about being
synchronized with respect to the thread that wrote the reference it
saw, not with regards to
other threads who wrote to that volatile.

This really is a corner case, and effects things such as whether you
can use volatiles
to force things like memory barriers and to implement subtle
concurrency idioms such
as the optimistic read idiom. Using the strong interpretation will
make a difference to 30
very clever people who will be able to use it to implement some subtle
but
efficient concurrent algorithms.

Bill

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:01:00 EDT