shared - i need it to be useful

Aliak something at something.com
Mon Oct 22 12:39:52 UTC 2018


On Monday, 22 October 2018 at 10:26:14 UTC, Timon Gehr wrote:
> ---
> module borked;
>
> void atomicIncrement(int* p)@system{
>     import core.atomic;
>     atomicOp!("+=",int,int)(*cast(shared(int)*)p,1);
> }
>
> struct Atomic(T){
>     private T val;
>     void opUnary(string op : "++")() shared @trusted {
>         atomicIncrement(cast(T*)&val);
>     }
> }
> void main()@safe{
>     Atomic!int i;
>     auto a=&[i][0];// was: Atomic!int* a = &i;
>     import std.concurrency;
>     spawn((shared(Atomic!int)* a){ ++*a; }, a);
>     ++i.val; // race
> }
> ---
>
>
> Oh no! The author of the @trusted function (i.e. you) did not 
> deliver on the promise they made!

hi, if you change the private val in Atomic to be “private shared 
T val”, is the situation the same?


More information about the Digitalmars-d mailing list