How to call stop from parallel foreach

Ali Çehreli acehreli at yahoo.com
Thu Jun 24 21:19:19 UTC 2021


On 6/24/21 1:41 PM, seany wrote:

 > Is there any way to control the number of CPU cores used in
 > parallelization ?

Yes. You have to create a task pool explicitly:

import std.parallelism;

void main() {
   enum threadCount = 2;
   auto myTaskPool = new TaskPool(threadCount);
   scope (exit) {
     myTaskPool.finish();
   }

   enum workUnitSize = 1; // Or 42 or something else. :)
   foreach (e; myTaskPool.parallel([ 1, 2, 3 ], workUnitSize)) {
     // ...
   }
}

I've touched on a few parallelism concepts at this point in a presentation:

   https://www.youtube.com/watch?v=dRORNQIB2wA&t=1332s

Ali



More information about the Digitalmars-d-learn mailing list