www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.concurrency: The fate of unmatched messages

reply "E.S. Quinn" <anonymous example.com> writes:
I'm putting together a program that uses std.concurrency to 
handle two child threads from the main thread;

The kicker here is that both the children do very different 
things. And I would like to handle receive() calls for them in 
separate locations. But from what I can tell, each thread has 
only one mailbox. And none of the documentation i can find for 
std.concurrency mentions what happens when one receive() call 
gets a message it doesn't understand. Are unmatched messages left 
in the Mailbox, or are they discarded? (I am admittedly strongly 
hoping it is the former)

And if the latter, is there any way to actually give a single 
thread multiple mailboxes?
Jul 10 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Fri, 10 Jul 2015 19:39:24 +0000, E.S. Quinn wrote:

 the documentation i can find for std.concurrency mentions what happens
 when one receive() call gets a message it doesn't understand.
that `receive()` will not get such a message. `receive()` scans the whole=20 mailbox to find the message it can process, and process only that,=20 leaving other messages in mailbox.
 And if the latter, is there any way to actually give a single thread
 multiple mailboxes?
why do you want that? you can receive any message with `receive`, see the=20 documentation[1]: "If a delegate that accepts a std.variant.Variant is included as the last=20 argument to receive, it will match any message that was not matched by an=20 earlier delegate. If more than one argument is sent, the Variant will=20 contain a std.typecons.Tuple of all values sent." receive( (int i) { writeln("Received an int."); }, (float f) { writeln("Received a float."); }, (Variant v) { writeln("Received some other type."); } ); this way your `receive` will get all messages. simply do nothing in=20 `Variant` handler to drop messages you don't want to process.
Jul 10 2015
parent reply "E.S. Quinn" <anonymous example.com> writes:
On Friday, 10 July 2015 at 23:39:30 UTC, ketmar wrote:
 this way your `receive` will get all messages. simply do 
 nothing in `Variant` handler to drop messages you don't want to 
 process.



The thing is, I want to do receive() in two separate places, and I want each receive() call to leave the other's messages alone, not drop them.
Jul 10 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Sat, 11 Jul 2015 01:52:23 +0000, E.S. Quinn wrote:

 On Friday, 10 July 2015 at 23:39:30 UTC, ketmar wrote:
 this way your `receive` will get all messages. simply do nothing in
 `Variant` handler to drop messages you don't want to process.



=20 The thing is, I want to do receive() in two separate places, and I want each receive() call to leave the other's messages alone, not drop them.
so simply don't receive the messages you don't need right now. as i said,=20 `receive()` doesn't look to top message only, it scans the whole mailbox,=20 trying to find a message that matches. you can use `receiveTimeout()` to=20 do nothing if there are no suitable messages. you can also adjust mailbox=20 size and mode.=
Jul 10 2015
parent "E.S. Quinn" <anonymous example.com> writes:
On Saturday, 11 July 2015 at 02:15:02 UTC, ketmar wrote:
 so simply don't receive the messages you don't need right now. 
 as i said, `receive()` doesn't look to top message only, it 
 scans the whole mailbox, trying to find a message that matches. 
 you can use `receiveTimeout()` to do nothing if there are no 
 suitable messages. you can also adjust mailbox size and mode.
Okay, so it doesn't purge unrecognized messages, then! That's what I needed to know!
Jul 10 2015