[concurrency-interest] Volatile array and Wait

Boehm, Hans hans.boehm at hp.com
Wed Oct 19 13:59:09 EDT 2005


I agree with Chris' later posting.  And interestingly, that works only
because Java totally orders synchronization operations (at the cost of
making volatile writes relatively expensive).

Another way to look at this is that for any synchronization free
statements X and Y:

X; dummy = v; Y;

can be transformed to

dummy = v; X; Y;

i.e. the compiler can safely move the volatile load to the beginning of
the synchronization-free block (or equivalently move the preceding
synchronization-free operations past the acquire operation).

In this particular case, this means that that dummy = b can effectively
moved to just after the read of b in the computation of b[22], and is
thus effectively a no-op.

Hans

> -----Original Message-----
> From: concurrency-interest-bounces at cs.oswego.edu 
> [mailto:concurrency-interest-bounces at cs.oswego.edu] On Behalf 
> Of Shaffer, Darron
> Sent: Wednesday, October 19, 2005 9:08 AM
> To: Jeremy Manson; Inanc Gumus
> Cc: Vijay; concurrency-interest at altair.cs.oswego.edu
> Subject: RE: [concurrency-interest] Volatile array and Wait
> 
> 
> The second option can use the volatile reference to the array 
> as the "dummy".
> 
> So given:
> 
>   volatile Object[] b = new Object[50];
> 
> reading looks like this:
> 
>    Object x = b[25];  // Volatile read must occur first, to 
> find the array.
> 
> writing looks like this:
> 
>    b[22] = x; Object dummy = b;  // Must have trailing volatile read.
> 
> If you do this, be sure and comment it well enough that a 
> junior/maintenance programmer doesn't decide that volatile 
> applies to the entire array.
>  
> 
> -----Original Message-----
> From: concurrency-interest-bounces at cs.oswego.edu
> [mailto:concurrency-interest-bounces at cs.oswego.edu] On Behalf 
> Of Jeremy Manson
> Sent: Wednesday, October 19, 2005 9:40 AM
> To: Inanc Gumus
> Cc: Vijay; concurrency-interest at altair.cs.oswego.edu
> Subject: Re: [concurrency-interest] Volatile array and Wait
> 
> Inanc Gumus wrote:
> > So, how to make inter array elements volatile?
> > 
> 
> There is no way to do this directly.  The simplest option is 
> probably to
> 
> use AtomicReferenceArray, AtomicIntegerArray or AtomicLongArray.
> 
> Another option is to have an extra volatile dummy field 
> associated with 
> the array.  After each write to an array element, write to the dummy 
> field (it doesn't matter what value is written).  Before each 
> read of an
> 
> array element, read the dummy field (and discard the value).
> 
> A third option is to use indirection.  That is to say, you 
> could create 
> an array of references to:
> 
>       class VolatileRef {
>         public volatile Object ref = null;
>      }
> 
> 
> A fourth option is to use locking instead.
> 
> 					Jeremy
> _______________________________________________
> Concurrency-interest mailing list 
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> 
> _______________________________________________
> Concurrency-interest mailing list 
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> 



More information about the Concurrency-interest mailing list