View mode: basic / threaded / horizontal-split · Log in · Help
August 27, 2010
std.concurrency : using delegate in lieu of function
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.
August 27, 2010
Re: std.concurrency : using delegate in lieu of function
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/
Top | Discussion index | About this forum | D home