Combining Unique type with concurrency module

Alex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 13 09:09:02 PDT 2015


Hi everybody! I'm new to this forum so, please excuse me in 
advance for asking silly questions. I think I'm not the first 
person which wondering about this topic, but I'm trying to 
combine Unique type and concurrency module, getting the compiler 
error

struct std.typecons.Unique!(S).Unique is not copyable because it 
is annotated with @disable

My test code is taken from the examples of the D docu from this 
page:

void spawnedFunc2(Tid ownerTid)
{
	receive(
		(ref Unique!S ur)
		{
			writeln("Recieved the number ", ur.i);
		}
	);
	send(ownerTid, true);
}

static struct S
{
     int i;
     this(int i){this.i = i;}
}

Unique!S produce()
{
     // Construct a unique instance of S on the heap
     Unique!S ut = new S(5);
     // Implicit transfer of ownership
     return ut;
}

void main()
{
     Unique!S u1;
     u1 = produce();
     auto childTid2 = spawn(&spawnedFunc2, thisTid);
     send(childTid2, u1);
     writeln("Successfully printed number.");
}

I'm aware of the fact, that my u1 struct can't be copied, but I 
don't intend to do so. As in the docu stated, I want to lend the 
struct to the other thread (by using ref), being sure, that any 
other thread can't access the struct during it is processed by 
the first one.
Is such a thing possible?
Thanks in advance.
Alex


More information about the Digitalmars-d-learn mailing list