www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Concurrency

reply JOEL <joel dlang.com> writes:
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);
      }

}
May 22 2020
parent reply Francesco Mecca <me francescomecca.eu> writes:
On Friday, 22 May 2020 at 14:31:05 UTC, JOEL wrote:
 [...]
These kind of questions should go to the Learn section of the forum. Also, what you are trying to accomplish is not exactly clear and the example code that you give doesn't compile without modifications. One possible cause of your error is the fact that you send a tuple (tid + int) not a single int. I tried to give a fixed version of your code that used receive(...) here: https://gist.github.com/FraMecca/2f062ed15eedd5320d403c5b518878c4 You should also signal to the other process that it should stop looping and exit, otherwise an exception get thrown.
May 22 2020
parent JOEL <joel dlang.com> writes:
On Friday, 22 May 2020 at 21:16:12 UTC, Francesco Mecca wrote:
 On Friday, 22 May 2020 at 14:31:05 UTC, JOEL wrote:
 [...]
These kind of questions should go to the Learn section of the forum. Also, what you are trying to accomplish is not exactly clear and the example code that you give doesn't compile without modifications. One possible cause of your error is the fact that you send a tuple (tid + int) not a single int. I tried to give a fixed version of your code that used receive(...) here: https://gist.github.com/FraMecca/2f062ed15eedd5320d403c5b518878c4 You should also signal to the other process that it should stop looping and exit, otherwise an exception get thrown.
Thanks alot
May 22 2020