Added shared static constructors/destructors, regular static constructors/destructors now deal with TLS
import std.stdio; import core.thread; class X { int v; } static X a; static this() { a = new X; a.v = 1; } shared static X b; shared static this() { b = new shared(X); b.v = 2; } class MyThread : Thread { this() { super(&run); } void run() { write("(a=", a.v, ")"); } } class MySharedThread : Thread { this() { super(&run); } void run() { synchronized { write("(b=", cast(int)b.v, ")"); } } } void main() { auto th1 = new MyThread; auto th2 = new MySharedThread; a.v = 10; b.v = 20; th1.start(); th2.start(); }
aはstaticグローバル変数なので宣言と同時に初期化ができない.
aの初期化はstatic this モジュールコンストラクタで行う.
同様に,dmd2.040では,shared staticグローバル変数 b を初期化するためのshared static this モジュールコンストラクタが追加された.
必然的な機能の追加だ.