Problem with receiveOnly and classes

Ghislain no_spam at no_spam.com
Sun Apr 1 03:53:55 PDT 2012


> Either way, I once tried to pass over "ownership" of trees of objects to
> other threads but gave up on that. I don't think std.concurrency and D
> really can work like that. (?) Concurrency seems to work much better
> when you pass messages and data, structs are fine, and then build object
> trees from that on the other side etc. Sorry I can't help more.

I finally found a solution, thanks to your post: 
make a struct holding the object (and alias it so I can use it "nearly"
transparently).

I do not really like it, but it solves my problem. 
Would it be possible to integrate this into std.concurrency? 
I am newbie in D (my background is C++)

Here is sample code:


import std.stdio;
import std.concurrency;
class A{
	public:
		void fun()const{ writeln("A.fun()");}
}
class B : A{
	public:
		override void fun()const{ writeln("B.fun()");}
}
immutable struct P(T){
	public:
		immutable(T) t;
		alias t this;
		this(immutable(T) _t)immutable{
			t = _t;
		}
}
void producer(Tid t){
	auto a = new immutable(A);
	auto b = new immutable(B);

	auto p1 = new P!A(a);
	auto p2 = new P!A(b);
	t.send(p1);
	t.send(p2);
}


void main(){
	auto t = spawn(&producer, thisTid);
	while(1){
		receive( 
			(P!A* a){
				writeln("matched P!A*"); 
				a.fun();
			},
			(Variant v){
				writeln("Unknown");
			}
			);
	}
}


More information about the Digitalmars-d-learn mailing list