```
__gshared int ctors, dtors;
static this() { ctors++; }
static ~this() { dtors++; }
import core.thread;
import core.stdc.stdio;
void main() {
ctors = 0;
dtors = 0;
createLowLevelThread({
scope(failure) assert(0); // satisfy nothrow
thread_attachThis();
rt_moduleTlsCtor();
rt_moduleTlsDtor();
thread_detachThis();
}).joinLowLevelThread;
printf("ctors %d dtors %d\n", ctors, dtors);
assert(ctors == 1 && dtors == 1);
}
```
with these commands, it works:
% dmd -run tlstest.d
% ldc2 -run tlstest.d
with these commands, the constructor and destructor don't run:
% dmd -defaultlib=phobos2 -run tlstest.d
% dmd -L-lphobos2 -run tlstest.d
% ldc2 --link-defaultlib-shared -run tlstest.d
other things that do TLS initialization like `new Thread()` and `rt_init()` work normally
Comment #1 by duser — 2022-01-16T21:53:15Z
slightly longer example with a C main showing `rt_init()`:
```
__gshared int ctors, dtors;
static this() { ctors++; }
static ~this() { dtors++; }
import core.runtime;
import core.thread;
import core.stdc.stdio;
extern(C) int main() {
rt_init();
printf("ctors %d dtors %d (should be 1 0) - main\n", ctors, dtors);
ctors = 0;
dtors = 0;
createLowLevelThread({
scope(failure) assert(0); // satisfy nothrow
thread_attachThis();
rt_moduleTlsCtor();
printf("ctors %d dtors %d (should be 1 0) - ll thread\n", ctors, dtors);
rt_moduleTlsDtor();
thread_detachThis();
}).joinLowLevelThread;
printf("ctors %d dtors %d (should be 1 1) - ll thread\n", ctors, dtors);
ctors = 0;
dtors = 0;
auto t = new Thread({
printf("ctors %d dtors %d (should be 1 0) - D thread\n", ctors, dtors);
});
t.start();
t.join();
printf("ctors %d dtors %d (should be 1 1) - D thread\n", ctors, dtors);
ctors = 0;
dtors = 0;
rt_term();
printf("ctors %d dtors %d (should be 0 1) - main\n", ctors, dtors);
return 0;
}
```
the only difference with dynamic phobos is that the constructors for the low-level thread don't run (OS threads with pthread are also affected)
`rt_init()` successfully inits them for the main thread, and so does `new Thread()` for the D thread
Comment #2 by robert.schadek — 2024-12-07T13:41:41Z