What exactly shared means?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 3 15:10:57 PST 2015


On Saturday, January 03, 2015 13:39:51 via Digitalmars-d-learn wrote:
> On Saturday, 3 January 2015 at 12:34:10 UTC, Jonathan M Davis via
> Digitalmars-d-learn wrote:
> > 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.
>
> I don't agree with this. If you avoid the problem by message
> passing, then you should do like Go and make it a language
> feature. And accept that you loose out on efficiency and restrict
> the application domain.
>
> If you make "shared" a language feature you also need to back it
> up with semantic analysis and prove that the concept is sound and
> useful (i.e. no need to break encapsulation, not making most
> libraries unusable without unsafe casting etc).

It is far better in general to use message passing to separate threads, but
by no means is it intended that it be the only way to do things in D. It is
a systems language after all. We have synchronized blocks and mutexes. We
have stuff like std.parallelism. We have fibers. There are quite a few ways
to go about having threads communicate and share data. It's just that
message passing is by far the cleanest and safest in most cases, so it's
what most code should be doing. If that's not what fits a particular
program, then there are plenty of other options available. However, that
does tend to mean having to deal with shared more.

And shared is definitely of benefit regardless of whether you're forced to
carefully cast it away some of the time to use it, just like C++'s const is
useful even if it's not actually full-on physical const like D's const is.
But we pretty much all agree that we want better guarantees than that.
Ideally, you would never cast away shared, and it would be cast away for you
by the compiler in sections of code where it can guarantee that it's safe to
do so (that was part of the idea behind synchronized classes). But that's
incredibly difficult to do, particularly in a useful way, so we don't
currently have it. And yes, that sucks, and we definitely want to fix it,
but I still think that it's far better than having everything be shared by
default like you get in languages like C++ and Java.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list