Spawn threads and receive/send

Luis Panadero Guardeño luis.panadero at gmail.com
Sun Apr 15 02:46:11 PDT 2012


I'm trying to implement a clock thread that sends messages and get 
blocked when the message queue it's full.

So I try this:
void func() {
  int n;
  while (1) {
    receive( (int i) { writeln(n, " : Received the number ", i); n++;} 
);
  }
}

void clock() {
  receive((Tid tid) {
    while (1) {
      writeln("sending...");
      send(tid, 42);
      Thread.sleep(dur!"msecs"(1000));
      
    }
  });
}

void main() {
  auto tid = spawn(&func);
  setMaxMailboxSize(tid, 1, OnCrowding.block);
  auto clock = spawn(&clock);
  send(clock, tid);

}

I get a screen full of "sending..." but func never receive any message.
But when I try this in main() :
void main() {
  auto tid = spawn(&func);
  setMaxMailboxSize(tid, 1, OnCrowding.block);
  while (1) {
    Thread.sleep(dur!"msecs"(1000));
    writeln("sending...");
    send(tid, 42); // Receive the result code.
  }
  //auto clock = spawn(&clock);
  //send(clock, tid);
}

It works and I get "sending..." and received pairs on screen.

Why is hapening this ?

Note: I try it with dmd 2.059 x64 and gdmd 4.6.3

-- 
I'm afraid that I have a blog: http://zardoz.es 


More information about the Digitalmars-d-learn mailing list