digitalmars.D.learn - Can the send function send an array?
- Fox (23/23) Sep 10 2024 // I am learning how to send and receive data. The following is
- Andy Valencia (6/10) Sep 10 2024 dlang tries to use the type system to make one be clear about
- Fox (25/25) Sep 10 2024 OK, It works, thanks.
// I am learning how to send and receive data. The following is
my intention, but it cannot be compiled.
// aliases to mutable thread-local data not allowed, what does it
mean? thank you.
I am learning how to send and receive. The following is my
intention, but it cannot be compiled. Report
import std.concurrency;
import std.stdio;
import std.exception;
void main(){
int N=5;
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); //error!
enforce(receiveOnly!Tid() == j);
}
void fun(){
auto msg5= receiveOnly!(Tid, int[])();
writeln("child thread, msg= ", msg5[1]);
msg5[0].send(thisTid);
}
Sep 10 2024
On Tuesday, 10 September 2024 at 13:14:05 UTC, Fox wrote:// I am learning how to send and receive data. The following is my intention, but it cannot be compiled. // aliases to mutable thread-local data not allowed, what does it mean? thank you.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
Sep 10 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]
Sep 10 2024








Fox <linuxl4 sohu.com>