The "no gc" crowd

Jonathan M Davis jmdavisProg at gmx.com
Thu Oct 10 19:04:16 PDT 2013


On Thursday, October 10, 2013 18:21:52 Andrei Alexandrescu wrote:
> On 10/10/13 5:36 PM, Jonathan M Davis wrote:
> > On Thursday, October 10, 2013 10:55:49 Andrei Alexandrescu wrote:
> >> On 10/10/13 12:33 AM, Jonathan M Davis wrote:
> >>> or write all of your code to
> >>> explicitly work with shared, which is not something that generally makes
> >>> sense to do unless you're creating a type whose only value is in being
> >>> shared across threads.
> >> 
> >> yes
> > 
> > Really? Do you honestly expect the average use of shared to involve
> > creating structs or classes which are designed specifically to be used as
> > shared?
> Yes. Data structures that can be shared are ALWAYS designed specifically
> for sharing, unless of course it's a trivial type like int. Sharing
> means careful interlocking and atomic operations and barriers and stuff.
> You can't EVER expect to obtain all of that magic by plastering "shared"
> on top of your type.

It works just fine with the idiom that I described where you protect the usage 
of the object with a lock, cast it to thread-local to do stuff on it, and then 
release the lock (making sure that no thread-local references remain). Aside 
from the necessity of the cast, this is exactly what is typically done in 
every C++ code base that I've ever seen - and that's with complicated types 
and not just simple stuff like int. 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.
}

I agree that designing types specifically to function as shared objects has its 
place (e.g. concurrent containers), but in my experience, that is very much 
the rare case, not the norm, and what most D programmers seem to describe when 
talking about shared is simply using __gshared with normal types, not even 
using shared, let alone using it with types specifically designed to function 
as shared. So, the most common approach at this point in D seems to be to 
avoid shared entirely.

- Jonathan M Davis


More information about the Digitalmars-d mailing list