dlangBugzillaToGithub / migration_test

0 stars 0 forks source link

std.parallelism.parallel doesn't enforce shared-correctness #940

Open dlangBugzillaToGithub opened 9 years ago

dlangBugzillaToGithub commented 9 years ago

schuetzm (schuetzm) reported this on 2015-09-30T08:40:40Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=15129

CC List

Description

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.
dlangBugzillaToGithub commented 1 year ago

nick commented on 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.
dlangBugzillaToGithub commented 1 year ago

nick commented on 2023-01-16T12:51:54Z

Both of these are examples of possible data races that should've been detected:
https://issues.dlang.org/show_bug.cgi?id=23624
https://issues.dlang.org/show_bug.cgi?id=23634