The text of this article is not in this database — only its details are. Read it on the old site: Implementing Singleton Design Patterns
3 Comments
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
The text of this article is not in this database — only its details are. Read it on the old site: Implementing Singleton Design Patterns
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
ApurvaPosted Jun 9, 2015, 10:41 PM
Hi Pankaj, using lock(_lock) we made the implementation thread-safe. If suppose we use directly if(instance == null) then there may come a scenario where two threads evalutes this statement as true and both the threads will end up creating an instance, which will violates the singleton pattern.
Pankaj Kumar ChoudharyPosted Jun 9, 2015, 10:13 AM
Hello Arurva can you explain what is diff b/w both public static Singleton GetInstance { get { //to make implementation thread-safe lock (_lock) { if (instance == null) { instance = new Singleton(); } return instance; } } } and public static Singleton GetInstance { get { if (instance == null) { instance = new Singleton(); } return instance; } }
Pankaj Kumar ChoudharyPosted Jun 9, 2015, 10:08 AM
Nice Start @Apurva .........