How to break from parallel foreach?

Jordan Wilson wilsonjord at gmail.com
Tue Feb 26 21:36:46 UTC 2019


On Tuesday, 26 February 2019 at 19:58:24 UTC, Andrey wrote:
> Hello,
> How to break from parallel foreach?
> More general question - how to control such loop?

A basic way would be to use a flag:

shared stopWork=false;
foreach (wordBag; wordBags.parallel) {
     if (!stopWork) {
         // do work
         if (wordBag.canFind("myword")) stopWork=true
     }
}

I'd say it's probably not an elegant solution to "break" from 
parallel forloops, but that's up to you to decide.

Jordan




More information about the Digitalmars-d-learn mailing list