[dmd-concurrency] shared arrays

Andrei Alexandrescu andrei at erdani.com
Wed Jan 27 15:25:49 PST 2010


Consider a shared array:

shared int[] x;

What can be done with it? It's tricky; the array has two words worth of 
info and we can't assume 128 bits can be atomically written on a 64-bit 
machine.

So you can't assign the entire array. If we assume no assignment, then 
element access with bounds checking becomes possible. But then we need 
to provide a method for changing a shared array. There are a few 
possibilities:

(a) recommend an extra indirection

shared int[]* x;

A pointer can be reassigned, so whenever you want to mess with the array 
you reallocate it.

(b) recommend you put the array in a class

class A
{
     int[] x;
}

Then, inside synchronized methods of shared(A) objects, the type of x 
changes from shared(int[]) to shared(int)[] and can be manipulated.

(c) recommend the yet-unwritten class std.concurrency.SharedArray.

Any thoughts are welcome.


Andrei


More information about the dmd-concurrency mailing list