Static constructors?

awishformore awishformore at nospam.plz
Wed Jul 21 17:00:21 PDT 2010


I have a question to static constructors in D2 and with threads.

-LogManager.d-

import std.stdio;

final class LogManager
{
	private:
		static __gshared LogManager instance;
		
	public:
	
		static this()
		{
			instance = new LogManager();
			writefln("New LogManager instance created.");
		}
	
		static ~this()
		{
			instance = null;
		}

		static LogManager opCall()
		{
			return instance;
		}
}

-end-

As stated in the docs, the static constructor is supposed to be executed 
before main. However, over the course of my application, every time I 
use the LogManager in a new thread, the static constructor will be 
executed again. Afaik this means that I have one static instance per 
thread, which is not at all which I would expect, especially considering 
the fact that instance is declared as __gshared (fyi: shared doesn't 
make a difference either).

Is this a bug? If not, could someone explain why the behaviour is different?

Thanks, Max.


More information about the Digitalmars-d-learn mailing list