Thread overview
How do I break from loop when using parallel()?
May 28, 2018
Dr.No
May 28, 2018
WebFreak001
May 28, 2018
Neia Neutuladh
May 29, 2018
Russel Winder
May 28, 2018
		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?

May 28, 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.

See https://dlang.org/phobos/std_parallelism.html
May 28, 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?

By the time you try to break out of the loop, you've already executed the loop body for later elements in the collection. So if you could do that, it would give the wrong impression.

The loop body executes on several threads at once. The `return false` statement might be executing on a different thread, and `return` only returns on the same thread.

If you want to do this sort of thing (exit on first error, for instance), you can manually use TaskPool, schedule tasks on it, and use an atomic variable to exit early on each task if necessary.
May 29, 2018
On Mon, 2018-05-28 at 21:04 +0000, Dr.No via Digitalmars-d-learn 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?

It isn't a loop, it is a task scatter/gather, with each task running to completion independent of all other tasks. Thus the concept of break/return doesn't exist.

It could be argued that this is a bad use of foreach, but it is what it is.

-- 
Russel.
===========================================
Dr Russel Winder      t: +44 20 7585 2200
41 Buckmaster Road    m: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk