How to initialize a globle variable nicely and properly?

Heromyth bitworld at qq.com
Sat Dec 15 13:49:03 UTC 2018


On Saturday, 15 December 2018 at 03:48:15 UTC, Neia Neutuladh 
wrote:
> On Sat, 15 Dec 2018 02:54:55 +0000, Heromyth wrote:
>>      shared static this() {
>>          writeln("running A in shared static this(),
>> sharedField=", sharedField);
>> 
>>          Thread th = new Thread(() {  });
>>          th.start();
>
> When you start a D thread, thread-local static constructors get 
> run. So don't start threads until your shared static 
> constructors finish.
>
> If I encountered something like this, I would set up a queue of 
> actions to be run at the start of main() and fill them with 
> static constructors. Instead of this static constructor 
> creating a thread, it would enqueue a delegate that starts the 
> thread.

Yes, it's very dangerous to create a new thread in shared static 
this(). For a big project, it sometimes hard to identify this 
problem. Maybe, the compiler should do something for this, should 
it?


More information about the Digitalmars-d-learn mailing list