core.sync.rwmutex example

Joshua Niehus via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 9 14:51:01 PDT 2014


Hi Charles,

would the following work (just a shot in the dark) ?

//---
module test;

import std.stdio;
import std.concurrency;

void spawnedFuncFoo(Tid tid, Tid tidBar) {
      receive(
          (int i) {
              writeln("Foo Received the number ", i);
              send(tidBar, i, thisTid);
              auto barSuccessful = receiveOnly!(string);
              writeln("Bar got my (Foo) message");
          }
      );

      send(tid, true);
}

void spawnedFuncBar(Tid tid) {
      receive(
          (int i, Tid tidFoo) {
              writeln("Foo passed me (Bar) the number ", i);
              send(tidFoo, "done");
          }
      );

      receive(
          (string sig) {
              writeln("Main says I'm (Bar) done.");
              send(tid, 42);
          }
      );
}

void main() {
      auto tidBar = spawn(&spawnedFuncBar, thisTid);
      auto tidFoo = spawn(&spawnedFuncFoo, thisTid, tidBar);
      send(tidFoo, 42);
      auto fooWasSuccessful = receiveOnly!(bool);
      assert(fooWasSuccessful);

      send(tidBar, "your done");
      auto barWasSuccessful = receiveOnly!(int);
      assert(barWasSuccessful == 42);
      writeln("Successfully had two separate threads communicate
with each other");
}
//---


More information about the Digitalmars-d-learn mailing list