xxxxxxxxxx
import core.atomic;
import core.stdc.stdio;
import core.stdc.stdlib;
struct S
{
static shared int* ptr;
this(int i)
{
ptr = cast(shared int*) malloc(4);
*ptr = 1;
}
this(this)
{
if (ptr && *ptr)
atomicOp!"+="(*ptr, 1);
}
~this()
{
if (ptr && *ptr)
atomicOp!"-="(*ptr, 1);
}
this(int r, int e)
{
this = sum(r, e);
}
static S sum(int r, int e)
{
return S(r + e);
}
void opAssign(S rhs)
{
if (ptr && *ptr)
atomicOp!"+="(*ptr, 1);
}
}
void main()
{
auto s = S(1, 2); // internally opAssign+dtor
auto x = s; // copy+postblit
{
auto y = x; // copy+postblit
} // dtor
assert(s.ptr && *s.ptr == 2);
}