The "no gc" crowd

Jonathan M Davis jmdavisProg at gmx.com
Thu Oct 10 18:05:07 PDT 2013


On Friday, October 11, 2013 02:08:16 Sean Kelly wrote:
> On Thursday, 10 October 2013 at 23:33:27 UTC, Jonathan M Davis
> 
> wrote:
> > Yeah. The only times that something is going to accept shared
> > is when it was
> > specifically designed to work as shared (which most code
> > isn't), or if it's
> > templated and the template happens to work with shared. Regular
> > functions just
> > aren't going to work with shared without casting away shared,
> > because that
> > would usually mean either templating everything or duplicating
> > functions all over the place.
> 
> I think that's pretty reasonable though. Shared data needs to be
> treated differently, explicitly, or things go downhill fast.

I'm not disagreeing with how shared works. I'm disagreeing with the idea that 
it's not supposed to be normal to cast shared away when operating on shared 
objects. I expect that the most common idiom for dealing with shared is to 
protect it with a lock, cast it to thread-local, do whatever you're going to 
do with it, make sure that there are no thread-local references to it once 
you're done operating on it, and then release the lock. e.g.

synchronized
{
 auto tc = cast(T)mySharedT;
 tc.memberFunc();
 doStuff(tc);
 //no thread-local references to tc other than tc should
 //exist at this point.
}

That works perfectly fine and makes it so that shared objects are clearly 
delineated from thread-local ones by the type system, but it does require 
casting, and it requires that you make sure that the object is not misused 
while it's not being treated as shared.

The only real alternative to that is to create types which are designed to be 
operated on as shared, but I would expect that to be the rare case rather than 
the norm, because that requires creating new types just for sharing across 
threads rather than using the same types that you use in your thread-local 
code, and I don't expect programmers to want to do that in the average case.

However, from what I've seen, at the moment, the most typical reaction is to 
simply use __gshared, which is the least safe of the various options. So, 
people need to be better educated and/or we need figure out a different design 
for shared.

- Jonathan M Davis


More information about the Digitalmars-d mailing list