[Issue 17476] Static fields don't seem to be reliably initialized when using parallel()

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Jun 7 09:48:58 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=17476

--- Comment #6 from Andrej Mitrovic <andrej.mitrovich at gmail.com> ---
> In main, you set the TLS instance of path corresponding to the main thread to "foobar". The "parallel" loop body will use the current thread's TLS instance, but because the order of execution of threads is of course undefined, you get inconsistent results.

I was initially confused not by the order of things, but by the multiple
printouts of "foobar". However this explains everything:

-----
import std.concurrency;
import std.stdio;
import std.parallelism;

struct Params
{
    static string path = "/some/string/initializer";
}

void main()
{
    Params.path = "foobar";

    foreach (_; parallel([1, 2, 3, 4]))
    {
        writefln("%s %s", thisTid(), Params.path);
    }
}
-----

Tid(7fe4c452e800) foobar
Tid(7fe4c452e800) foobar
Tid(7fe4c452e900) /some/string/initializer
Tid(7fe4c452ea00) /some/string/initializer

It's a non-issue, we were just puzzled at first. :)

--


More information about the Digitalmars-d-bugs mailing list