How to call stop from parallel foreach

Ali Çehreli acehreli at yahoo.com
Fri Jun 25 14:10:52 UTC 2021


On 6/25/21 6:53 AM, seany wrote:

 > I tried this .
 >
 >                  int[][] pnts ;
 >          pnts.length = fld.length;
 >
 >          enum threadCount = 2;
 >          auto prTaskPool = new TaskPool(threadCount);
 >
 >          scope (exit) {
 >              prTaskPool.finish();
 >          }
 >
 >          enum workUnitSize = 1;
 >
 >          foreach(i, fLine; prTaskPool.parallel(fld, workUnitSize)) {
 >                    //....
 >                  }
 >
 >
 > This is throwing random segfaults.
 > CPU has 2 cores, but usage is not going above 37%

Performance is not guaranteed depending on many factors. For example, 
inserting a writeln() call in the loop would make all threads compete 
with each other for stdout. There can be many contention points some of 
which depending on your program logic. (And "Amdahl's Law" applies.)

Another reason: 1 can be a horrible value for workUnitSize. Try 100, 
1000, etc. and see whether it helps with performance.

 > Even much deeper down in program, much further down the line...
 > And the location of segfault is random.

Do you still have two parallel loops? Are both with explicit TaskPool 
objects? If not, I wonder whether multiple threads are using the 
convenient 'parallel' function, stepping over each others' toes. (I am 
not sure about this because perhaps it's safe to do this; never tested.)

It is possible that the segfaults are caused by your code. The code you 
showed in your original post (myFunction0() and others), they all work 
on independent data structures, right?

Ali



More information about the Digitalmars-d-learn mailing list