basic concurrency

Tydr Schnubbis fake at address.dude
Tue Apr 18 08:13:59 PDT 2006


Frank Benoit wrote:
> Tydr Schnubbis schrieb:
>> If I have an array and want to synchronize all access to it (only one
>> thread using it at a time), what's the best way?  I know how to use
>> 'synchronized' to make a critical section, but I want to synchronize
>> data, not code.
> 
> i am not completely sure, but does this make sense?
> 
> if you want to access the array consitently, you have to synchronize
> multiple accesses.
> 
> int[10] a;
> a[0] = a[0] +1;
> => how can the data sychronization know that there should be a lock
> until the write?
> 
> I would write a class, encapsulating the data and all accesses to them.
> So you can use the existing thread synchronisation.
> 
Putting the array inside a class was what I was thinking about.  Let's 
say I have this:

class MyArray {
   private int[] array;

   int get(size_t i)
   {
     return array[i];
   }

   void append(int v)
   {
     array ~= v;
   }
}

How do I use 'synchronized' to make sure get() and append() can't be 
called simultaneously by two or more different threads?



More information about the Digitalmars-d-learn mailing list