Confusing TLS

Jack Applegame japplegame at gmail.com
Tue Mar 13 22:34:32 UTC 2018


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



More information about the Digitalmars-d mailing list