On Monday, 6 May 2013 at 02:35:33 UTC, dsimcha wrote:
On the advice of Walter and Andrei, I've written a blog article about the low-lock Singleton pattern in D. This is a previously obscure pattern that uses thread-local storage to make Singletons both thread-safe and efficient and was independently invented by at least me and Alexander Terekhov, an IBM researcher. However, D's first-class treatment of thread-local storage means the time has come to move it out of obscurity and possibly make it the standard way to do Singletons.

Article:
http://davesdprogramming.wordpress.com/2013/05/06/low-lock-singletons/

Reddit:
http://www.reddit.com/r/programming/comments/1droaa/lowlock_singletons_in_d_the_singleton_pattern/

FWIW, I played with a generalized form of this pattern long ago, something like (typing from memory):

template sharedGlobal(alias ctor, alias lock = globalLock)
{
     @property sharedGlobal()
     {
         alias ReturnType!ctor T;
         __gshared static Nullable!T t;
         static bool instantiated;

         if (!instantiated)
         {
             synchronized(lock)
             {
                 if (t is null)
                     t = ctor();
             }
     }

     return cast(T)t;
}

alias sharedGlobal!({ return new Blah(); }) blah;

It should have been part of QtD but that has never happened.

Reply via email to