Sharing Ref Counted Containers

Jonathan M Davis jmdavisprog at gmail.com
Wed Aug 4 18:55:33 PDT 2010


On Wednesday 04 August 2010 18:20:36 Andrei Alexandrescu wrote:
> On 08/04/2010 08:02 PM, dsimcha wrote:
> > I noticed that std.container.Array uses completely unsynchronized
> > reference counting.  Does that mean that it's basically impossible to
> > share it across threads without introducing race conditions into the
> > reference counting code, even if you synchronize on other updates?
> > 
> > Are there any plans to make Array and future ref counted containers
> > shareable?
> > 
> >   If not (I'm not saying that making them shareable is necessarily worth
> > 
> > doing), I think there should be a large warning against casting these to
> > shared.  Usually, when you cast a data structure to shared, you can write
> > working code as long as you properly synchronize all updates to the data
> > structure.  However, because updates to the ref count field are happening
> > under the hood in an invisible fashion, no matter how properly
> > synchronized updates to the contents of the ref-counted container are,
> > you basically can't properly synchronize updates to the ref count field.
> 
> There are no plans to make Array a shared type. A shared container would
> have a very different interface to begin with. Sharing Array objects is
> not recommended.
> 
> Andrei

I would have thought that sharing pretty much _any_ type more complex than a 
primitive type without it being synchronized would be a bad idea. And since the 
standard containers obviously aren't going to be synchronized, I would expect 
you to have to wrap them in a class or struct which is synchronized.

- Jonathan M Davis


More information about the Digitalmars-d mailing list