Concurrency

JOEL joel at dlang.com
Fri May 22 14:31:05 UTC 2020


This code(below) using two threads, main() and square() which 
communicates by sending values.

main() generate a series of unique numbers(printed), and square() 
prints the squared of each serie it gets from.

Am trying to get the same effect , using :

  receive (
     (int num)(...),
   );

Instead of receiveOnly (Tid)(int)

But i get stuck communicating back to main.

Code:
     /*Assuming modules included */

    void main(){
       auto tid = spawn(& square);
       //Series
       foreach(i, 0..100){
         stdout.writef("No %d \t ", i);
         tid.send(thisTid,i);
      enforce(receiveOnly!(Tid)== tid);
       }
  }

//Now the sqaure
void square(){
     for(;;){
        auto msg = receiveOnly (Tid,int)();

    stdout.writef("square is %d\n", msg[1]^^2);
      msg[0].send(thisTid);
      }

}


More information about the Digitalmars-d mailing list