What To Do About Shared?

Don nospam at nospam.com
Wed Mar 23 12:40:41 PDT 2011


dsimcha wrote:
> == Quote from Don (nospam at nospam.com)'s article
>> Seems to me, that you're making use of some primitive that I'll call a
>> 'DivisableArray' -- an array that can be sliced up (into other
>> DivisibleArrays), and different DivisableArrays can be sent to different
>> threads. You can extract a normal array slice from a DivisibleArray, but
>> cannot send that slice to other threads: only DivisibleArrays can do that.
>> In debug mode, a DivisibleArray could keep track of how it has been
>> sliced, and disallow overlapping slices. The DivisibleArray could even
>> ensure that all slices lie on word/paragraph boundaries, thus dealing
>> with word tearing.
> 
> I'm not sure.  I assume that the implementation would rely on a shared array, plus
> casting away shared under the hood when you extract a normal array.  In this case,
> you could still have a DivisibleArray aliased to an unshared slice into this
> array.  This is not safe IIUC.

No. I haven't said anything about the implementation of DivisibleArray 
(or even that it is implementable in the language at the present time). 
Haven't thought about it that much. It's more a thought experiment, 
trying to imagine a magical type would which might solve the big issues.
Stream of consciousness below...

It wouldn't be a shared array. In fact, it would explicitly NOT be 
shared (If it's shared, some other thread somewhere else could be 
clobbering it while the operation's happenning!). What it would be, 
would be a (non-shared) array which guarantees that no overlapping 
slices of that type exist. It would hand out slices which are guaranteed 
to be unique.

So in practice, it'd need to be a reference type, so that it remained 
valid when copied (preventing you from copying it, then slicing in two 
different places).
I guess it would need to construct the array itself, since that's the 
only way it could guarantee uniqueness. That's an inefficiency. Data 
could be copied into it and out of it, though.
One problem is, that once it's given out a non-const slice for the data, 
it can't safely be split. It could allow slice and index assignment 
itself, though.

I think those are the main implementation challenges.


More information about the Digitalmars-d mailing list