digitalmars.D - std.concurrency : using delegate in lieu of function
- Gabriel Huau (28/28) Aug 27 2010 Hello,
- Michel Fortin (10/22) Aug 27 2010 But by passing the this pointer of your Foo object to another thread,
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
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








Michel Fortin <michel.fortin michelf.com>