Wednesday, June 1, 2016

Thread-safe Singleton Pattern (Multi-threaded Singleton Pattern)

In my previous post Singleton Pattern, I explained various ways to implement singleton pattern but those were not thread-safe. In multi-threaded environment it’s quite possible that multiple thread enters into Instance property at the same time and more than one object of Singleton class may be created. In this article I'll show you various ways to create thread-safe singleton pattern.

Simple version of thread-safe singleton pattern via locking.


Code -
public sealed class Singleton
{
    //private static member.
    private static Singleton instance;
    private static object objLock = new object();

    //Private constructor prevents instance creation of class from outside.
    private Singleton() { }

    //This static property responsible for creation of instance of this class.
    public static Singleton Instance
    {
        get
        {
            lock (objLock)
            {
                if (instance == null)
                {
                    Console.WriteLine("New Instance created");
                    instance = new Singleton();
                }
            }
            return instance;
        }
    }

    public void Display()
    {
        Console.WriteLine("Display Method called from Singleton Class...");
    }
}


class Program
{
    static void Main(string[] args)
    {
        //array of threads
        Thread[] thrd = new Thread[4];

        for (int i = 0; i < 4; i++)
        {
            thrd[i] = new Thread(new ThreadStart(() =>
            {
                Singleton singleObject = Singleton.Instance;
                singleObject.Display();
            }));
            thrd[i].Start();
        }
        Console.Read();
    }
}
  

Output – 

You can see “New Instance created” message displayed only once since new instance created only once across multiple thread calls. The above example allows only single thread to enter instance creation code when no instance created. The lock block allows only single thread to enter and create an instance. 

In this approach lock is acquired every time the instance of class is requested by user so it will impact performance. To avoid an exclusive locking in every call of Instance property, you can use double-check locking approach.

Double-check locking approach


Code –
public sealed class Singleton
{
    //private static member.
    private static volatile Singleton instance;
    private static object objLock = new object();

    //Private constructor prevents instance creation of class from outside.
    private Singleton() { }

    //This static method responsible for creation of instance of this class.
    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (objLock)
                {
                    if (instance == null)
                    {
                        Console.WriteLine("New Instance created");
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }

    public void Display()
    {
        Console.WriteLine("Display Method called from Singleton Class...");
    }
}
   
class Program
{
    static void Main(string[] args)
    {
        //array of threads
        Thread[] thrd = new Thread[4];

        for (int i = 0; i < 4; i++)
        {
            thrd[i] = new Thread(new ThreadStart(() =>
            {
                Singleton singleObject = Singleton.Instance;
                singleObject.Display();
            }));
            thrd[i].Start();
        }
        Console.Read();
    }
}

Output - 

Same as above example.

In double-check locking approach, Only first thread acquires lock and creates object while other threads will not be able to acquire lock since I added check before lock statement. So this way we can reduce acquiring lock for each and every thread who calls Instance property and improve performance. I have declared instance variable as volatile so it will read from physical memory every time instead of thread's internal cache.

Thread-safe without using locks via static initialization


As per above example, we can implement thread-safe singleton pattern using locks but there is another way to create thread-safe singleton pattern without using locks but via static member initialization. This approach does not support lazy initialization of an object like above example does.

Code –
namespace ThreadSafeSingleton
{
    public sealed class Singleton
    {
        private static Singleton instance = new Singleton();
       
        private Singleton() { }

        public static Singleton Instance
        {
            get
            {
                return instance;
            }
        }

        public void Display()
        {
            Console.WriteLine("Display Method called from Singleton Class...");
        }
    }
}

namespace ThreadSafeSingleton
{
    class Program
    {
        static void Main(string[] args)
        {
            //array of threads
            Thread[] thrd = new Thread[4];

            for (int i = 0; i < 4; i++)
            {
                thrd[i] = new Thread(new ThreadStart(() =>
                {
                    Singleton singleObject = Singleton.Instance;
                    singleObject.Display();
                }));
                thrd[i].Start();
            }
            Console.Read();
        }
    }
}

Output – 









With this approach, new instance is created only once when any static member or method of this class is referenced. This uses static variable initializer approach and CLR takes care of it. In this approach you have very less control over the creation of the object.

Hope this article helps you to understand different ways to implement thread-safe singleton pattern. Please leave your feedback.

References –

See Also – 


Tuesday, May 31, 2016

Singleton Pattern


Singleton pattern ensures a class can have only single instance during the lifetime of an application.

When to use Singleton Pattern?

When you want to create single object only once during application life you can go with Singleton Pattern. The implementation and use of singleton pattern is very easy. See below example.

Code –
public class Singleton
{
    //private static member.
    private static Singleton instance;
   
    //Private constructor prevents instance creation of class from outside.
    private Singleton() { }
   
    //This static method responsible for creation of instance of this class.
    public static Singleton GetInstance()
    {
        //create new instance only in first call, rest of the time it will return existing object
        if (instance == null)
            instance = new Singleton();

       return instance;
    }

    public void Display()
    {
        Console.WriteLine("Display Method called from Singleton Class...");
    }
}
class Program
{
    static void Main(string[] args)
    {
        Singleton singleObject =  Singleton.GetInstance();
        singleObject.Display();
        Console.Read();
    }
}

Output –



Another way (via Property) to implement singleton pattern.


public class Singleton
{
    //private static member.
    private static Singleton instance;

    //Private constructor prevents instance creation of class from outside.
    private Singleton() { }

    //This static method responsible for creation of instance of this class.
    public static Singleton Instance
    {
        get
        {
            //create new instance only in first call, rest of the time it will return existing object
            if (instance == null)
                instance = new Singleton();

            return instance;
        }
    }

    public void Display()
    {
        Console.WriteLine("Display Method called from Singleton Class...");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Singleton singleObject = Singleton.Instance;
        singleObject.Display();
        Console.Read();
    }
}

Output –

Same as above example.

Advantages - 

  • Class can perform additional functionalities other than creation of an object.
  • This is referred as Lazy Initialization approach since instance of an object is not created until user call instance property or method. This approach avoids instantiating singletons class when application starts.

Disadvantage -

  • Singleton pattern is not thread safe by default. You need to write additional code to make it thread safe. I'll post article for Thread safe singleton pattern in few days.

Hope you liked this article. Please leave your feedback.

References –


See Also –