Pushkar
Pushkar

Reputation: 109

Multi threading in case of singleton class C#

I want to know in case of multi threading how singleton works. Suppose 2 threads enter the instantiation code as shown in below code , and 1st thread enters the instantiation code it locks that part and proceeds with its operation till that time another thread waits. So once the first thread completes its operation 2nd thread will enter the instantiation code , now I want to know who takes the responsibility to release the lock since 1st thread has completed its operation and will second thread create new instance or will it share 1st threads instantiation???

Code :

public sealed class Singleton
{
    private static Singleton instance = null;
    // adding locking object
    private static readonly object syncRoot = new object();
    private Singleton() { }

    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
}

Upvotes: 0

Views: 6020

Answers (3)

Ravi Kumar Kasim
Ravi Kumar Kasim

Reputation: 174

One more best way to implement this we can create a static constructor it will be good.

Upvotes: 1

Peter Duniho
Peter Duniho

Reputation: 70652

once the first thread completes its operation 2nd thread will enter the instantiation code , now I want to know who takes the responsibility to release the lock since 1st thread has completed its operation

Each thread acquires and releases the lock individually. The first thread acquires the lock; while it has the lock, the second thread cannot acquire it.

Once the first thread has released the lock (which happens when execution leaves the block of code controlled by the lock statement), the second thread can then acquire the lock and execute the code. It will then release the lock again, when it's done with it.

…and will second thread create new instance or will it share 1st threads instantiation???

In this particular implementation, the singleton is initialized only once, even if the second thread initially observes the backing field to be null. By using lock, the code ensures that only one thread will ever actually create the singleton instance.

There are variations in which more than one initialization could occur, but IMHO these are inferior ways to do it.

That said, for that matter, it's my opinion that in the context of .NET, even the double-lock above is needlessly complicated. In most cases, it suffices to use a simple field initializer. In the cases where that's not efficient enough, one can use the Lazy<T> class.

For a much more in-depth discussion, see the related Stack Overflow question Thread Safe C# Singleton Pattern and references provided there.

Upvotes: 1

Cory Nelson
Cory Nelson

Reputation: 29981

What you've used here is called double-check locking, a fairly common serialization pattern for multi-threaded code. It works.

A lock is released automatically once you fall out of the lock scope.

Assuming there is contention, one thread would test->acquire->test->initialize->release, and the next would simply test->acquire->test->release: no double-initialization.

Upvotes: 0

Related Questions