Help with Concurrency

JR via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Nov 4 08:49:51 PST 2015


On Tuesday, 3 November 2015 at 23:16:59 UTC, bertg wrote:
> Running the following code I get 3 different tid's, multiple 
> "sock in" messages printed, but no receives. I am supposed to 
> get a "received!" for each "sock in", but I am getting hung up 
> on "receiving...".

[...]

>         while (true) {
>             writeln("receiving...");
>             std.concurrency.receive(
>                 (string msg) {
>                     writeln("conn: received ws message: " ~ 
> msg);
>                 }
>             );
>             writeln("received!");
>         }

Unless I'm reading it wrong, you want 
std.concurrency.receiveTimeout.

     import core.time;
     import std.concurrency;

     bool received = receiveTimeout(1.seconds, // negative makes 
it not wait at all
         (string msg) {
             writeln("conn: received ws message: " ~ msg);
         });

         if (received) {
             writeln("received!");
         }
         else {
             writeln("timed out...");
             // stuff?
         }
     }

> Tries to receive but will give up if no matches arrive within 
> duration. Won't wait at all if provided core.time.Duration is 
> negative.


More information about the Digitalmars-d-learn mailing list