can I force a parallel foreach to finish?
McAnany, Charles E
mcanance at rose-hulman.edu
Fri Jul 22 15:38:15 PDT 2011
Hi, all. So I'm getting the classic "concurrency noob" behavior from this code:
shared int times;
int[] iterationRange = new int[2500];
foreach (pos, ref i; parallel(iterationRange)){
times++;
}
writeln(times);
}
Prints random numbers near 1,000.
Looking at the documentation for std.concurrency, it appears that when dealing with tasks, you have to yieldForce to get them to finish. Is there a method that blocks until taskPool itself is empty?
I think the problem is not concurrent modification, the foreach just seems to return too early, as seen here:
int times;
void shareAugmenter(){
bool cont = true;
while(cont){
receive( (int i){times++;},
(string s){writefln("in the thread %s",times);cont = false;});
}
}
void main(string[] args){
auto td = spawn(&shareAugmenter);
int[] iterationRange = new int[2500];
foreach (pos, ref i; parallel(iterationRange)){
td.send(1);
}
writeln(times);
td.send("");
writeln(times);
prints
0
0
In the thread 2500
Cheers,
Charles.
More information about the Digitalmars-d-learn
mailing list