On Friday, 31 January 2014 at 08:25:16 UTC, Andrej Mitrovic wrote:
class LockSingleton
{
    static LockSingleton get()
    {
        __gshared LockSingleton _instance;

        synchronized
        {
            if (_instance is null)
                _instance = new LockSingleton;
        }

        return _instance;
    }

private:
    this() { }
}

Should't be the LockSingleton implemented like this instead?

class LockSingleton
{
    static auto get()
    {
        if (_instance is null)
        {
            synchronized
            {
                if (_instance is null)
                    _instance = new LockSingleton;
            }
        }

        return _instance;
    }

private:
    this() { }
    __gshared LockSingleton _instance;
}

At least this is the way singleton is suggested to implement in C#, because synchronization is then needed only for initial instantiation and not allways.

Reply via email to