www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.concurrency : using delegate in lieu of function

reply Gabriel Huau <contact huau-gabriel.fr> writes:
Hello,

I would like to know, why the module std.concurrency use function()
parameter in lieu of delegate ? 

Delegate would be usefull for example to pass Function Member to
spawn().

Example :

class Foo
{
	void test() 
	{	
		for(;;)
		{
			receive(
				(string s) { writeln("string"); }
			);
		}
	
	}
}

void main()
{
	Foo l = new Foo();
	void delegate() dg;

	dg = &l.test;
	auto a = spawn(dg);

	a.send("test");
}

I have tested and it worked.
Aug 27 2010
parent Michel Fortin <michel.fortin michelf.com> writes:
On 2010-08-27 12:53:07 -0400, Gabriel Huau <contact huau-gabriel.fr> said:

 void main()
 {
 	Foo l = new Foo();
 	void delegate() dg;
 
 	dg = &l.test;
 	auto a = spawn(dg);
 
 	a.send("test");
 }
 
 I have tested and it worked.
But by passing the this pointer of your Foo object to another thread, you've broken the rules about sharing memory between threads. If Foo was a shared or synchronized type, then this could be made to work safely (with some adaptation to make the compiler aware of what delegates can be safely shared.) -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Aug 27 2010