January 05, 2012
how can i implementing the singleton pattern in D?
January 05, 2012
On Thu, Jan 5, 2012 at 4:15 PM, asm <asm@hotmail.com> wrote:
> how can i implementing the singleton pattern in D?

Multithreaded or not?
January 05, 2012
both if you don't mind
January 05, 2012
On 01/05/2012 02:15 PM, asm wrote:
> how can i implementing the singleton pattern in D?

Is singleton still alive? ;)

An idea is to instantiate the object in the module's static this().

Ali
January 06, 2012
On Thursday, January 05, 2012 22:15:32 asm wrote:
> how can i implementing the singleton pattern in D?

The same way that you would in C++ or Java, except that unlike you have static constructors that you can use to initialize them, and unlike both, if it's not shared, then it's a singleton per thread and no locking is required.

If you're dealing with a multi-threaded singleton, technically, it should be possible to use double-checked locking with shared, but apparently bugs with shared make that unsafe at present. So, if you didn't initialize it in a shared static constructor, either you lock every time that you fetch the singleton or you use an extra bool for indicating that it's been initialized.

Single threaded:

T singleton()
{
 static T instance;

 if(!instance)
 instance = new T();

 return instance;
}

Multi-threaded:

shared(T) singleton()
{
 static shared T instance;
 static bool initialized = false;

 if(!initialized)
 {
 synchronized
 {
 if(!instance)
 instance = new shared(T)();
 }

 initialized = true;
 }

 return instance;
}

Once the bugs with shared with regards to fences have been fixed though, the shared version can look more like

shared(T) singleton()
{
 static shared T instance;

 if(!instance)
 {
 synchronized
 {
 if(!instance)
 instance = new shared(T)();
 }
 }

 return instance;
}

Regardless, if you use a shared static constructor, you can avoid the locking (it's just not lazily initialized then), and of course, the singly threaded one can use a static constructor if you don't care about lazy initialization. If you use a static constructor though, the instance would not be declared inside the singleton function (and it doesn't have to be here either - it's just shorter to do it this way in an example).

- Jonathan M Davis
January 06, 2012
thank you, you answer is very helpful.
January 12, 2012
On Friday, January 6, 2012, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> The same way that you would in C++ or Java, except that unlike you have
static

The most simple and elegant way in java is to use an enum. Is there nothing comparable in D?


January 12, 2012
On Thursday, January 12, 2012 00:55:14 Thomas Mader wrote:
> On Friday, January 6, 2012, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> > The same way that you would in C++ or Java, except that unlike you have
> 
> static
> 
> The most simple and elegant way in java is to use an enum. Is there nothing comparable in D?

No, because enums must be known at compile time. This avoids the ordering issues that you get in languages like C++ or Java when you directly initialize static variables like that. So, to have an enum which is a class, you need to be able to construct a class at compile time and have it persist into the runtime of the program. And while you _can_ now use classes with CTFE, they cannot persist beyond CTFE (though that may be possible eventually). Any class which is used at runtime must be constructed at runtime. So, either you need to use a static constructor, or you need to initialize the singleton lazily.

- Jonathan M Davis
Top | Discussion index | About this forum | D home