digitalmars.D.learn - Possible to do receive on a delegate?
- Bienlein (25/25) Feb 19 2014 Hello,
- John Colvin (5/30) Feb 19 2014 delegate
- Bienlein (24/28) Feb 19 2014 Thanks for the hint! This works now:
- Tobias Pankrath (2/5) Feb 19 2014 A nested function has a context pointer to its outer scope, like
- John Colvin (3/28) Feb 19 2014 Not helpful right now, but maybe interesting to you:
Hello,
I was wondering whether it can be done somehow to select on a
delegate in the receive block when spawning a thread:
void spawnedFunc(Tid tid)
{
// Receive a message from the owner thread.
receive(
(int i) { writeln("Received the number ", i);}
(delegate d) { writeln("Receiving a delegate");}
);
}
int delegate() dg;
void main()
{
// Start spawnedFunc in a new thread.
auto tid = spawn(&spawnedFunc, thisTid);
// Send the number 42 to this new thread.
send(tid, 42);
int a = 7;
int foo() { return a + 3; }
dg = &foo;
send(tid, dg);
}
My solution of course doesn't compile.
Thanks for any hint, Bienlein
Feb 19 2014
On Wednesday, 19 February 2014 at 16:23:36 UTC, Bienlein wrote:
Hello,
I was wondering whether it can be done somehow to select on a
delegate in the receive block when spawning a thread:
void spawnedFunc(Tid tid)
{
// Receive a message from the owner thread.
receive(
(int i) { writeln("Received the number ", i);}
(delegate d) { writeln("Receiving a delegate");}
);
}
int delegate() dg;
void main()
{
// Start spawnedFunc in a new thread.
auto tid = spawn(&spawnedFunc, thisTid);
// Send the number 42 to this new thread.
send(tid, 42);
int a = 7;
int foo() { return a + 3; }
dg = &foo;
send(tid, dg);
}
My solution of course doesn't compile.
Thanks for any hint, Bienlein
delegate
isn't a type. try
int delegate()
instead
Feb 19 2014
On Wednesday, 19 February 2014 at 17:05:01 UTC, John Colvin wrote:delegate isn't a type. try int delegate() insteadThanks for the hint! This works now: void spawnedFunc(Tid tid) { receive( (int i) { writeln("Received the number ", i);}, (int function() fp) { writeln("Receiving a function");} ); }; int function() fp; void main(string[] args) { auto tid = spawn(&spawnedFunc, thisTid); int a = 7; static int foo() { return 3; } fp = &foo; tid.send(fp); } A delegate wouldn't compile. I guess it is because it could reference data outside the thread which would result in the thread reaching into memory of the calling thread. I don't really understand why foo has to be static to compile. But this is really nice now :-). -- Bienlein
Feb 19 2014
I don't really understand why foo has to be static to compile. But this is really nice now :-). -- BienleinA nested function has a context pointer to its outer scope, like a delegate. static makes it an ordinary function.
Feb 19 2014
On Wednesday, 19 February 2014 at 16:23:36 UTC, Bienlein wrote:
Hello,
I was wondering whether it can be done somehow to select on a
delegate in the receive block when spawning a thread:
void spawnedFunc(Tid tid)
{
// Receive a message from the owner thread.
receive(
(int i) { writeln("Received the number ", i);}
(delegate d) { writeln("Receiving a delegate");}
);
}
int delegate() dg;
void main()
{
// Start spawnedFunc in a new thread.
auto tid = spawn(&spawnedFunc, thisTid);
// Send the number 42 to this new thread.
send(tid, 42);
int a = 7;
int foo() { return a + 3; }
dg = &foo;
send(tid, dg);
}
My solution of course doesn't compile.
Thanks for any hint, Bienlein
Not helpful right now, but maybe interesting to you:
http://forum.dlang.org/post/olhrismoxqybbcfujwju forum.dlang.org
Feb 19 2014









"Tobias Pankrath" <tobias pankrath.net> 