This compiles:
ulong i = 0;
foreach (f; parallel(iota(1, 1000000+1)))
{
i += f;
}
thread_joinAll();
i.writeln;
The delegate to which the function body is converted has mutable references to thread-local data, and should therefore be rejected.
If parallel takes its delegate via scope (as it should), and there are no aliases to the context passed to parallel() (e.g. as a second parameter), it is sufficient for the context to be const, otherwise it probably needs to be immutable.
Comment #1 by nick — 2023-01-16T12:15:15Z
> it is sufficient for the context to be const, otherwise it probably needs to be immutable.
`i` should be declared `shared` for safe mutation. To support that, the ParallelForeach.opApply delegate context parameter should require `shared` (which would allow `immutable` too). Unfortunately the opApply delegate doesn't support `shared` (or `immutable`) context inference AFAICT.