== Quote from Justin Johansson ([email protected])'s article
> Which language out of C++, D and Java does this classical
> "GoF" (gang-of-four***) design pattern best?
> *** http://en.wikipedia.org/wiki/Design_Patterns
> For my take, I prefer synchronization-free implementations.
> Surely this topic has been around before???
> <bystander awareness="clueless" name="Justin"/>
D makes it very easy to create an efficient thread-safe Singleton
implementation,
using a method that was inspired by broken double-checked locking, but uses
thread-local storage to be correct. The following algorithm guarantees that the
synchronized block guarding the Singleton will be entered at most once per
thread
throughout the lifetime of the program. I invented it myself, but it's fairly
simple and might have been independently invented elsewhere:
class Singleton {
private:
static bool initialized; // Thread-local
__gshared static Singleton instance;
this() {}
public:
static Singleton getInstance() {
if(initialized) {
return instance;
}
synchronized(Singleton.classinfo) {
scope(success) initialized = true;
if(instance !is null) {
return instance;
}
instance = new Singleton;
return instance;
}
}
}