Sharing in D

Sean Kelly sean at invisibleduck.org
Fri Aug 1 12:11:23 PDT 2008


== Quote from Jason House (jason.james.house at gmail.com)'s article
> Walter Bright Wrote:
> > Jason House wrote:
> > > Walter Bright Wrote:
> > >
> > >> Unshared can be implicitly cast to shared, so you shouldn't have
> > >> to.
> > >
> > > That should not be implicit.
> > >
> > > For one, thread-local garbage collection of non-shared data would
> > > leave dangling references to garbage memory.
> >
> > True, but the memory allocation system shouldn't be (and isn't) built
> > that way.
> I don't see how memory allocation fits into it. It's about deallocation and garbage collection. I believe
the single-threaded garbage collection would only scan memory for the thread in question and would
miss shared data's reference to non-shared data.

I think the only way this can work is if you make 'shared' a bit like 'pure' in that a shared
object can't reference non-shared data.  Then you have a full-on "stop the world" GC for
the shared data area.  With this approach you'd at least know that most of your GC cycles
would be thread-local, which is better than none of them.  If the user wants to bypass
this they can always cast unshared to shared when assigning a shared reference to it.
This gets back to what I said about shared being transitive.  The sticky part would be
enforcing it--I guess you'd have to do so at the point of assignment:

   class C
    {
        int* x;
        shared void put( int* y )
        {
            x = y; // error, y is not "shared(int*)"
        }
    }

It really is a lot like const, isn't it?


Sean



More information about the Digitalmars-d mailing list