Thread overview
Possible to do receive on a delegate?
Feb 19, 2014
Bienlein
Feb 19, 2014
John Colvin
Feb 19, 2014
Bienlein
Feb 19, 2014
Tobias Pankrath
Feb 19, 2014
John Colvin
February 19, 2014
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
February 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
February 19, 2014
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
February 19, 2014
> 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.
February 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