Help with Concurrency
Nicholas Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Nov 3 17:27:56 PST 2015
On Tuesday, 3 November 2015 at 23:16:59 UTC, bertg wrote:
> I am having trouble with a simple use of concurrency.
>
> 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...".
>
> Am I misusing or misunderstanding the use of mailboxes?
>
> ===
>
> class Connection {
> Reactor reactor;
> WebSocket webSocket;
>
> this(Reactor r, WebSocket ws)
> {
> reactor = r;
> webSocket = ws;
>
> messageLoop();
> }
>
> void messageLoop()
> {
> std.concurrency.Tid tid = std.concurrency.thisTid();
> writeln("tid 1 ~ " ~
> to!string(std.concurrency.thisTid()));
> writeln("tid 2 ~ " ~
> to!string(std.concurrency.thisTid()));
> writeln("tid 3 ~ " ~
> to!string(std.concurrency.thisTid()));
>
> // deal with websocket messages
> spawn(&handleConnectionWebSocket, tid, cast(shared)
> webSocket);
> // deal with pub/sub
> //spawn();
>
Try replacing the following loop to have a receive that times out
or while(true) to while(web socked.connected)
> while (true) {
> writeln("receiving...");
> std.concurrency.receive(
> (string msg) {
> writeln("conn: received ws message: " ~
> msg);
> }
> );
> writeln("received!");
> }
> }
> }
> void handleConnectionWebSocket(std.concurrency.Tid caller,
> shared WebSocket ws)
> {
> auto sock = cast(WebSocket) ws;
> while (sock.connected) {
> writeln("sock in");
> auto msgIn = sock.receiveText();
> std.concurrency.send(caller, msgIn);
> }
> }
More information about the Digitalmars-d-learn
mailing list