Re: JavaMemoryModel: volatile arrays

From: Bill Pugh (pugh@cs.umd.edu)
Date: Thu Dec 02 1999 - 16:59:57 EST


Under my proposal, this would be a data race. The fact that the
thread doing the writing does a sync is only visible to other threads
doing a sync on the same object.

Say that a compiler determined that only one thread ever invoked the
set method. Would it then be safe to remove the synchronization? If
so, then the expectation that this would work would fail.

This would also be flagged as a data race by any known technique for
data race detection.

        Bill

>Here's the example again, removing "final" to avoid syntax issues.
>
>class VolatileArray {
> private volatile int[] data;
> public VolatileArray(int cap) { data = new int[cap]; }
>
> public int get(int i) {
> int[] d = data;
> return d[i];
> }
>
> public void set(int i, int value) {
> int[] d = data;
> synchronized(d) { d[i] = value; }
> }
>}
>
>
>I'm less sure about whether Bill's LC proposal covers this case.

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



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