Helping with __mutable (which will be renamed to __metadata)

Suleyman sahmi.soulaimane at gmail.com
Sat Apr 13 03:45:35 UTC 2019


On Saturday, 13 April 2019 at 00:45:07 UTC, Andrei Alexandrescu 
wrote:
> ...

The problems you encountered are normal but the question is 
whether this feature is useful at all if the field is not 
explicitly shared? If so the a solution to the const case is to 
introduce an '@unshared' attribute instead which would protect 
against the worst cases of both being shared and being thread 
local. this attribute would disallow unsychronized read/write and 
at the same time prevent implicit conversion to 'shared' since it 
may just be thread local hence mixing it with normal 'shared' may 
not bring good results.

example:
```
struct S
{
     private shared int* p; // or @unshared

     void inc() inout pure @system
     {
         import core.atomic;
         atomicOp!"+="(*cast(shared(int*))p, 1); // or @unshared
     }
}

void foo(ref immutable S a) pure
{
     a.inc();
}

void foo(ref const S a) pure
{
     a.inc();
}

int x; // thread local
shared int y;

void main()
{
     auto i = immutable S(cast(immutable)&x);
     auto c = const S(&y);
     //auto d = const S(cast(@unshared)&x);
     foo(i);
     foo(c);
}
```



More information about the Digitalmars-d mailing list