0

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;
        }
    }
}
Pushkar
  • 99
  • 1
  • 3
  • 8
  • 1
    Possible duplicate of [Thread Safe C# Singleton Pattern](http://stackoverflow.com/questions/12316406/thread-safe-c-sharp-singleton-pattern) – Sanket Apr 16 '17 at 06:05
  • If applicable, consider using dependency injection instead of singleton. A container instantiates all classes that should have a single object, and those are injected via ctor to the classes that require them. Unity is one framework that supports this in C# – Itsik Apr 16 '17 at 06:41

3 Answers3

1

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.

Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
1

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

0

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.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110