shared - i need it to be useful

Manu turkeyman at gmail.com
Thu Oct 18 04:04:31 UTC 2018


On Wed, Oct 17, 2018 at 6:15 AM Timon Gehr via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> On 17.10.2018 14:24, Timon Gehr wrote:
> > and unshared methods are only allowed to access unshared members.
>
> This is actually not necessary, let me reformulate:
>
> You want:
>
> - if you have a C c and a shared(C) s, typeof(s.x) == typeof(c.x).

No. c.x is mutable, s.x is shared (and inaccessible).

> - shared methods are not allowed to access unshared members.

No. Shared methods are not allowed to access ANY members. Shared data
has no access.
I don't believe there's any rules that can make raw access to shared
data safe. We must depend on threadsafe tooling (libraries).

> - shared is not transitive, and therefore unshared class references
> implicitly convert to shared class references

No. Shared is transitive.

> Applied to pointers, this would mean that you can implicitly convert
> int* -> shared(int*), but not shared(int*)->int*, int* -> shared(int)*
> or shared(int)* -> int*.

Correct. This is acceptable because shared(int)* can not be accessed;
can't produce to a race.
shared -> unshared conversion produces an unshared alias, and is
obviously invalid.

> shared(int*) and shared(shared(int)*) would be
> different types, such that shared(int*) cannot be dereferenced but
> shared(shared(int)*) can.

No. No manner of shared(T) is safely accessible under any
circumstance. This is just the reality, and there's no way it can be
allowed.
The escape hatch is that T may have some shared methods which
implement threadsafe interaction with T, and THAT is the only safe way
to interact with shared data.


More information about the Digitalmars-d mailing list