Re: JavaMemoryModel: Semantics of volatile

From: Doug Lea (dl@cs.oswego.edu)
Date: Fri Mar 24 2000 - 07:02:06 EST


OK, now that I think I can live with V4, back to others.

> V6
> // Does a volatile variable guard unrelated fields?
> Initially:
> boolean a = false;
> volatile boolean b = false;
>
> Thread 1:
> a = true
> b = true
>
> Thread 2:
> while (!b);
> boolean tmp = a;
>
> Question: Is it possible that thread 2 gets a value of false in tmp?

I'm still uncomfortable that a read/write of a simple volatile boolean
is required to perform additional unrelated memory synchronization.

Note however, that people can get the effects of V6 via V5 if, instead
of using a scalar for the "guarding" volatile, they use a reference.
This provides more controllability since reads/writes of simple
scalars would not require full cache refreshes etc, while read/writes
of references/arrays still would.

To minimally twist your V6 example:

  Initially:
  class X {
    boolean a = false
    volatile Object b = null;
    ...
  }

  Thread 1:
  a = false;
  b = this; // fields of "this" are now visible.

  Thread 2:
  while (b == null);
  boolean tmp = a;

  Question: Is it possible that thread 2 gets a value of false in tmp?

Does this eliminate, or make significantly more difficult, any
of the idioms you had in mind?

(For reference, here's V5)
> V5
> // Does volatile guarantee fresh values for direct contents of
> referenced object
> // through multiple levels of indirection?
> Initially:
> volatile Node p;
>
> Thread 1:
> Node tmp1 = new Node();
> Node tmp2 = new Node();
> tmp1.next = tmp2;
> tmp2.x = 1;
> p = tmp1;
>
> Thread 2:
> int tmp3 = p.next.x;
>
> Question: Is it possible that thread 2 gets a value of 0 in tmp3?
>

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



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