Skip to main content
  1. Blog/

HowTo: Thread safe singleton implementation of a class

·2 mins

Normally, if you would like a singleton implementation of a class, you would go about it a little bit like this:

public class BloggingAbout
{
   
private static BloggingAbout bloggingAbout;
   
    private BloggingAbout()
    {
    }

    public static BloggingAbout BloggingAboutInstance
    {
        get
       
{
            if (bloggingAbout == null)
            {
                bloggingAbout = new BloggingAbout();
            }
            return bloggingAbout;
        }
    }
}

<p>
  </font></font></font>The trouble here is that&nbsp;this implementation&nbsp;is not thread safe. The reason I post this is I recently ran into some code of&nbsp;an old colleague&nbsp;of mine (from my former employee)&nbsp;and some tests showed his singleton class was instantiated several times when the application was stressed with requests&nbsp;from the start, which caused unwanted lag in the application. Apparently him not implementing the Singleton pattern thread safe caused this, so I fixed it.<br /> It's not thread safe because two different threads could both&nbsp;evaluate&nbsp;<font color="#0000ff">if</font><font size="2"> (bloggingAbout == </font><font color="#0000ff" size="2">null</font><font size="2">)&nbsp;</font>and find it to be true and create an&nbsp;instance, which violates the singleton pattern. To make sure this doesn't happen, you could use <a href="https://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrflockstatement.asp" target="_blank">locking</a> which would result in this code:
</p>

<p>
  <font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">class</font><font size="2"> BloggingAbout<br /> {<br /> &nbsp;&nbsp;&nbsp; </font><font color="#0000ff" size="2">private</font><font size="2"> </font><font color="#0000ff" size="2">static</font><font size="2"> BloggingAbout bloggingAbout;<br /> &nbsp;&nbsp;&nbsp; <font color="#0000ff" size="2">private</font><font size="2"> </font><font color="#0000ff" size="2">static</font><font size="2"> </font><font color="#0000ff" size="2">object</font><font size="2"> lockObject = </font><font color="#0000ff" size="2">new</font><font size="2"> </font><font color="#0000ff" size="2">object</font><font size="2">();</font><br /> &nbsp;&nbsp;&nbsp;&nbsp;<br /> &nbsp;&nbsp;&nbsp; <font color="#0000ff" size="2">private</font><font size="2"> BloggingAbout()<br /> </font></font><font size="2"><font size="2">&nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp; }</p> 
  
  <p>
    <font color="#0000ff" size="2">&nbsp;&nbsp;&nbsp; public</font><font size="2"> </font><font color="#0000ff" size="2">static</font><font size="2"> BloggingAbout BloggingAboutInstance<br /> &nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff" size="2">get<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font></font><font size="2">{<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff" size="2">lock</font><font size="2">(lockObject)<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font><br /> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff" size="2">if</font><font size="2"> (bloggingAbout == </font><font color="#0000ff" size="2">null</font><font size="2">) <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bloggingAbout = <font color="#0000ff" size="2">new</font><font size="2"> BloggingAbout();<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff" size="2">return</font><font size="2"> bloggingAbout;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp; }<br /> }</p> 
    
    <p>
      The lock statement ensures that one thread does not enter a critical section of code while another thread is in that critical section. If another thread attempts to enter a locked section of code, it will wait (block) until the object is released. This is the reason the lockObject has to exist even before calling the static property BloggingAboutInstance.</font></font></font></font>
    </p>
    
    <p>
      </font></font></font>
    </p>
    
    <p>
      </font></font>
    </p>
    
    <p>
      </font>
    </p>