shared - i need it to be useful

Manu turkeyman at gmail.com
Tue Oct 16 17:22:22 UTC 2018


On Tue, Oct 16, 2018 at 2:25 AM Kagamin via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> On Monday, 15 October 2018 at 18:46:45 UTC, Manu wrote:
> > Current situation where you can arbitrarily access shared
> > members
> > undermines any value it has.
>
> The value of shared is existence of thread-local data that's
> guaranteed to be not shared, so you don't need to worry about
> thread-local data being shared, and shared protects that value
> well.

This isn't really an argument in favour of shared being able to
arbitrarily access all its members though. I'm not sure how your
points addresses my claim?

this sounds like that old quote "there can not be peace without war".
It's not shared that makes not-shared thread-local by default, it's
just that's how D is defined.
Shared allows you to break that core assumption. Without shared, it
would still be thread-local by default... you just wouldn't have an
escape hatch ;)

> > Assuming this world... how do you use shared?
>
> Unique solution for each case.
>
> > If you write a lock-free queue for instance, and all the
> > methods are
> > `shared` (ie, threadsafe), then under the current rules, you
> > can't
> > interact with the object when it's not shared, and that's fairly
> > useless.
>
> Create it as shared.

Then you can't use it locally.

> > Assuming the rules above: "can't read or write to members", and
> > the understanding that `shared` methods are expected to have
> > threadsafe implementations (because that's the whole point),
> > what are the risks from allowing T* -> shared(T)* conversion?
>
> All data becomes possibly shared, so you can't assume it's
> unshared, effectively C-style sharing. BTW D supports the latter
> already.

No data becomes 'possibly shared', because it's all inaccessible.
Only if your object specifies a threadsafe API may some controlled
data become shared... and that's the whole point of writing a
threadsafe object.
If the threadsafe object doesn't support sharing its own data, then
it's not really a threadsafe object.


More information about the Digitalmars-d mailing list