Confusing TLS

Jacob Carlborg doob at me.com
Thu Mar 15 10:26:57 UTC 2018


On Tuesday, 13 March 2018 at 22:34:32 UTC, Jack Applegame wrote:
> Code:
>
> import std.stdio;
> import core.thread;
> import core.time;
>
> auto arr = new ubyte[1]; // THIS SHOULD NOT COMPILE
>
> int main(string[] args)
> {
>     new Thread({
>         arr[0]++;
>     }).start();
>
>     new Thread({
>         arr[0]++;
>     }).start();
>
>     Thread.sleep(1.seconds);
>     writefln("arr[0] = %s", arr[0]);
>     return 0;
> }
>
> https://glot.io/snippets/ez54cl239e
>
> In fact, it works as it should. But this behavior confuses 
> beginners. They expect each thread to work with its own copy of 
> the TLS-array.
> I believe, such initialization of mutable TLS-variables should 
> lead to a compilation error, as is done for classes:
>
> class Foo {}
> Foo foo = new Foo(); // doesn't compile

Make sense. Have you reported a bug?

--
/Jacob Carlborg


More information about the Digitalmars-d mailing list