Labels

Sunday, May 27, 2007

Thread Safe Version of SingleTon

Most common way of creating Singleton class :

private static SingletonClass Instance = null;

public static SingletonClass GetInstance()

{

if (Instance == null)

Instance = new SingletonClass();

return Instance;

}

But this method is not thread safe! Imagine two threads simultaneously checking if the instance of the class is null, when it is actually null! Both of these threads will create instance of our singleton class which is not what we want.

The Thread Safe way of creating a singleton class:

private static SingletonClass Instance = null;

private static readonly object lockingObject = new object();

public static SingletonClass GetInstance()

{

//Lock the shared object and then check for creating the class instance

lock (lockingObject)

{

if (Instance == null)

Instance = new SingletonClass();

return Instance;

}

}

Now this way of creating the singleton class is completely thread safe but what about the performance issues? Unfortunately, every time we want an instance of this class, we have to acquire the lock which affects the performance badly if our application is a heavily threaded application.

Improved: The thread safe, but a not so lazy implementation of singleton class:

private static SingletonClass Instance = new SingletonClass(); // Note: Here the non-static constructor will take precedence over static constructor.

// Either use Static or Private constructor.

// Private - Advantage- Does not forces to make any member variable[ required to be referred] in constructor to be static

//static SingletonClass()

//{

//}

private SingletonClass()

{

}

public static SingletonClass GetInstance()

{

return Instance;

}

How does this work?

In C# when a constructor is marked as static, they are specified to execute only when an instance of the class is created or a static member is referenced. Also, they are supposed to execute only once per AppDomain. This allows us to remove our locks because the checks required have already taken care of because of the property of the static constructor.

Now imagine if you have another static variable in the singleton class that you wish to access before you want to access the variable “Instance”? So, our “Instance” object creation wouldn’t be lazy anymore. (By lazy we mean, instantiated only when referenced)

But, in most of our implementations, we can do with this kind of an static instantiation of the singleton class because generally they do not have other static objects which we would wish to refer separately J

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

1 comment:

  1. Here is the Singleton Implementation:

    private static TokenManager tokenManagerInstance = new TokenManager();

    private TokenManager()
    {}

    public static TokenManager GetInstance()
    {
    return tokenManagerInstance;
    }

    ReplyDelete