How do I break from loop when using parallel()?

WebFreak001 at
Mon May 28 21:35:50 UTC 2018

On Monday, 28 May 2018 at 21:04:21 UTC, Dr.No wrote:
> 		import std.parallelism : parallel;
> 		foreach(t; parallel(arr))
> 		{
> 			if(!doSomething(t)) {
> 				return false;
> 			}
> 		}
> It reuturns the run time error:
>> std.parallelism.ParallelForeachError@(0): Cannot break from a 
>> parallel foreach loop using break, return, labeled 
>> break/continue or goto statements.
> What's the proper way to break from loop?

you can't break out because other tasks would still continue to 
finish while the loop is breaking. So either you could use 
`throw` to error out or you could simply create a `bool isDone;` 
and at the start of the loop do `if (isDone) continue;` to make 
all next tasks finish quickly (though CPU usage will be very high 
for a moment, depending on the list size)

If you want total control and break manually (with an uncertainty 
of a few iterations) you can probably create a custom taskPool 
and use the .stop member function on it.


More information about the Digitalmars-d-learn mailing list