Order of Static Construction

Rubn where at is.this
Fri Jan 4 15:46:47 UTC 2019


On Friday, 4 January 2019 at 13:27:29 UTC, Dru wrote:
> from the spec:
> "Shared static constructors on all modules are run before any 
> non-shared static constructors."
>
> Is there a specific reason, or is it just for simplicity?
>
> There could be a situation where a shared ctor depends on a 
> thread local variable.
> Preferably, the variable is only initialized in a non-shared 
> ctor.

The shared static constructor would only be able to access one of 
the thread-local variables (probably from the main thread). That 
isn't very useful. It'd be more useful to have thread local 
static constructors access variables that were constructed in the 
shared static constructor. The thread constructor is called any 
time a new thread is created, and you can create a new thread any 
time at runtime. It'd be impossible to guarantee the shared 
static constructor is called after all the thread local 
constructors unless you put a bunch of restrictions on threads.


More information about the Digitalmars-d mailing list