Can the send function send an array?

Fox linuxl4 at sohu.com
Tue Sep 10 15:28:36 UTC 2024


OK, It works, thanks.

// dlang tries to use the type system to make one be clear about 
what data is shared between potentially concurrent threads.
// You need that data to be "shared" before you can send it 
between threads.
// Andy

import std.concurrency;
import std.stdio;

void main(){

         int N=10;
         shared int[] arr1=new int[N];
         for(int i; i<N; i++){arr1[i]=i;}
         writeln("Main thread, msg= ", arr1);
         auto j=spawn(&fun);
         j.send(thisTid, arr1);
         assert(receiveOnly!Tid()==j);

}

void fun(){
         auto msg= receiveOnly!(Tid, shared int[])();
         writeln("child thread, msg= ", msg[1]);
         msg[0].send(thisTid);
}

./a.out
Main thread, msg= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
child thread, msg= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]






More information about the Digitalmars-d-learn mailing list