www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Possible to do receive on a delegate?

reply "Bienlein" <jeti789 web.de> writes:
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
next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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
parent reply "Bienlein" <jeti789 web.de> writes:
On Wednesday, 19 February 2014 at 17:05:01 UTC, John Colvin wrote:
 delegate

 isn't a type. try

 int delegate()

 instead
Thanks 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
parent "Tobias Pankrath" <tobias pankrath.net> writes:
 I don't really understand why foo has to be static to compile. 
 But this is really nice now :-).

 -- Bienlein
A nested function has a context pointer to its outer scope, like a delegate. static makes it an ordinary function.
Feb 19 2014
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
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