Static constructors?

Steven Schveighoffer schveiguy at yahoo.com
Thu Jul 22 07:40:30 PDT 2010


On Wed, 21 Jul 2010 22:27:10 -0400, awishformore <awishformore at nospam.plz>  
wrote:

> On 22/07/2010 03:36, Sean Kelly wrote:
>> Make the ctors "shared static this()" -- those are only constructed  
>> once when the process starts up.  The non-shared static ctors are  
>> thread-local.
>
> That concept is really weird, though. So this applies to anything  
> static, not just variables?

The puzzle is, let's say you have thread local static variables and shared  
static variables.  Which should be initialized when?

The end result we came to is that there should be a way for thread-local  
variables to be initialized on thread creation, and shared variables to be  
initialized on program instantiation.

So we need a syntax for this.  Various things were suggested, and the  
current behavior was chosen for its consistency with current terminology:

shared int x;
shared static this() { x = 5; }

or:

shared
{
   int x;
   static this() { x = 5; }
}

int x2;
static this() { x2 = 5; }

It looks pretty good to me.  You just have to start thinking that  
thread-local is the norm :)

-Steve


More information about the Digitalmars-d-learn mailing list