[concurrency-interest] Double Checked Locking in OpenJDK

Chris Povirk cpovirk at google.com
Wed Aug 15 21:25:21 EDT 2012

Thanks, that's definitely part of the picture.  My memory-model
knowledge is very rusty, and it was never great, so here's one more
round of questions.  First, I'll spell out some in-thread
happens-before relationships:

> local = new Vector();
> synchronized (local) {}
> shared = local;

construction happens before synchronized block
synchronized block happens before write
(=> construction happens before write)

> // other thread:
> local = shared;
> if (local != null) {
>   synchronized (local) {}
>   // safe to use local now?
> }

read happens before synchronized block
synchronized block happens before use of local
(=> read happens before use of local)

The other thing that we want is for the write to happen before the
read.  If it does, then we have construction HB write HB read HB use,
and we're fine.  But the only cross-thread happens-before
relationships I can imagine are conditional: *If* the writer's
synchronized block happens before the reader's synchronized block,
then yes, construction happens before use.  But if the *reader*'s
synchronized block happens before the *writer*'s synchronized block,
then all bets are off.  Or does that somehow guarantee that *none* of
the writes from the writer thread are visible to the reader,
guaranteeing that the reader can't see an incompletely initialized
object by guaranteeing that it can't see the object at all?  Another
(more likely?) possibility is that the writer's synchronized block is
guaranteed to happen before the reader's, but I don't know enough
rules to justify that.  Or is there a third option?

Thanks again.

More information about the Concurrency-interest mailing list