www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Spawn threads and receive/send

reply Luis Panadero =?UTF-8?B?R3VhcmRlw7Fv?= <luis.panadero gmail.com> writes:
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 
Apr 15 2012
parent reply Luis Panadero =?UTF-8?B?R3VhcmRlw7Fv?= <luis.panadero gmail.com> writes:
Auto response :

the main thread ends, and It signal func to end, so It never receive 
any message from clock. I fix it, doing that main sleep 1000 seconds 
after sending func Tid to clock

Luis Panadero Guardeño wrote:

 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
Apr 15 2012
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/15/2012 03:03 AM, Luis Panadero Guardeño wrote:
 Auto response :

 the main thread ends, and It signal func to end, so It never receive
 any message from clock. I fix it, doing that main sleep 1000 seconds
 after sending func Tid to clock
This too should work at the end of main(): thread_joinAll(); Ali
Apr 15 2012