What exactly shared means?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 3 04:34:00 PST 2015


On Saturday, January 03, 2015 12:14:54 via Digitalmars-d-learn wrote:
> On Saturday, 3 January 2015 at 00:12:35 UTC, Jonathan M Davis via
> Digitalmars-d-learn wrote:
> > In D, if a type is not marked as shared, then it is by
> > definition
> > thread-local, and the compiler is free to assume that it's
> > thread-local.
>
> I find this to be rather vague. If the compiler exploit this to
> the maximum wouldn't that lead to lots of bugs?

Only if you're declaring shared or __gshared variables and not protecting
them properly or interacting with C or C++ code in a way that doesn't take
their memory model into account. The vast majority of D code won't care one
whit and won't have any problems, because very little of it needs to be
shared, and thread communication most typically is done via message passing
using std.concurrency, not by declaring shared variables. So, the risk of
bugs related to the compiler taking advantage of its knowledge that a
variable is thread-local if not mark as shared is pretty low in most cases.

Yes, there's definitely a risk of bugs if you're casting away shared or
use __gshared and screw it up (which is part of why we'd much rather have a
way for the language to safely strip away shared when it can guarantee that
a shared variable is properly protected), but for the most part, it's not a
problem at all and is _far_ less of a problem than what you get in languages
like C or C++ which default to shared rather than thread-local.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list