Sharing in D

JAnderson ask at me.com
Sat Aug 2 14:06:57 PDT 2008


Jason House wrote:
> JAnderson Wrote:
>  
>> My thought was you could use Ref counting for shared memory and mark and 
>>   sweep for unshared.
> 
> That's a really good idea. Mark and sweep (as currently implemented in D) can be very expensive with multiple threads.

Thanks,

A couple more thoughts.  With a naive approach a lock would have to be 
acquired every time something was referenced counted in the unshared. 
There are 2 ways I can think of to counter this:

1) Have a thread local ref count.  Then the shared memory would simply 
have a ref count of the number threads that have access to that piece of 
memory.  When the memory is first required it would acquire a lock and 
increment the count in the shared as well as the local.  From then on it 
would only increment/decrement its local ref count until it reached 0 
then it would have to update the shared one again.

2) The local thread doesn't contain a ref count but instead has a proxy 
memory for each shared memory reference.  When it goes to delete the 
proxy it tells the shared memory to dec its count.

3) Perhaps ref counting isn't need after all.  All that is really needed 
is a table in the shared memory that simply details what threads have 
what pieces of memory.  When the shared GC runs it will also go over 
that list and see that some pieces of memory are still being held on to. 
  Local threads would only need to lock/unlock that portion of memory. 
The shared GC would no be ableble to run while this memory is being 
accessed.  The advantage here is that each thread has its own bit of 
memory in the shared to work with.  Of course race conditions will still 
be a problem to solve.

-Joel



More information about the Digitalmars-d mailing list