Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7933101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:13:31+00:00 2026-06-03T21:13:31+00:00

using System; public sealed class Singleton { private static volatile Singleton instance; private static

  • 0
using System;

public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

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

         return instance;
      }
   }
}

I don’t understand why is the double check !
I read that this double check is to solve the thread concurrency problems – but …

  1. The lock will solve it – so why we need to first ‘if’

  2. if this singleton will be without the first ‘if’ it still will be thread safe – right ?

  3. if the first ‘if’ is false – so thread1 will init the ‘instance’ object => now, the ‘instance’ is not null and the thread1 is still in the lock block ===>> now, thread2 check the first ‘if’ and will get false => so he will not get to the ‘lock’ and imminently will return the instance and thread2 is able to change properties of the ‘instance’ => so thread1 && thread2 are ‘working’ on same ‘instance’ object => so where is the thread safe … or what I’m missing here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:13:32+00:00Added an answer on June 3, 2026 at 9:13 pm

    1.The lock will solve it – so why we need to first ‘if’

    So you won’t lock the thred unless you need to create the new instance of the Singleton.
    lock is a very expensive operation, so it’s worth doing that extra check.

    2.if this singleton will be without the first ‘if’ it still will be thread safe – right ?

    Yes, but significantly slower.

    3.Thread1 && thread2 are ‘working’ on same ‘instance’ object => so where is the thread safe

    This is the whole point of singleton, only one instance for all threads. That’s the thread safe thing…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using System.Collections.Generic; public sealed class LoLQueue<T> where T: class { private SingleLinkNode<T> mHe; private
using System; public class ImageConverter { public void button1_Click(Object sender, RoutedEventArgs e) { string
using System; using System.Math; class test { public static void Main() { Console.Write(Enter any
I have some code looking like this: using System.Threading; public sealed class MyClass :
using System; using System.Runtime.InteropServices; using System.Windows.Forms; public static class Program { [STAThread] public static
using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; public sealed class Foo { public int ID;
using System; namespace random { interface IHelper { void HelpMeNow(); } public class Base
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace EfTestFactory { public abstract class
The winform: The code: using System; using System.Windows.Forms; namespace DemoApp { public partial class
How can I generate types like these using the System.Reflection.Emit libraries: public class Test<T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.