www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Confusing TLS

reply Jack Applegame <japplegame gmail.com> writes:
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
Mar 13 2018
next sibling parent matthewwade <matthewwade482 gmail.com> writes:
Ensure yourself and your clients' information. Get it today to 
comprehend compelling and effective 
confirmation.https://www.courseworkempire.co.uk/
Mar 14 2018
prev sibling parent Jacob Carlborg <doob me.com> writes:
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
Mar 15 2018