HowTo: Thread safe singleton implementation of a class
By rickvdbosch
- 2 minutes read - 361 wordsNormally, if you would like a singleton implementation of a class, you would go about it a little bit like this:
<p>
</font></font></font>The trouble here is that this implementation is not thread safe. The reason I post this is I recently ran into some code of an old colleague of mine (from my former employee) and some tests showed his singleton class was instantiated several times when the application was stressed with requests 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 evaluate <font color="#0000ff">if</font><font size="2"> (bloggingAbout == </font><font color="#0000ff" size="2">null</font><font size="2">) </font>and find it to be true and create an 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 /> </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 /> <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 /> <br /> <font color="#0000ff" size="2">private</font><font size="2"> BloggingAbout()<br /> </font></font><font size="2"><font size="2"> {<br /> }</p>
<p>
<font color="#0000ff" size="2"> public</font><font size="2"> </font><font color="#0000ff" size="2">static</font><font size="2"> BloggingAbout BloggingAboutInstance<br /> {<br /> <font color="#0000ff" size="2">get<br /> </font></font><font size="2">{<br /> <font color="#0000ff" size="2">lock</font><font size="2">(lockObject)<br /> {</font><br /> <font color="#0000ff" size="2">if</font><font size="2"> (bloggingAbout == </font><font color="#0000ff" size="2">null</font><font size="2">) <br /> {<br /> bloggingAbout = <font color="#0000ff" size="2">new</font><font size="2"> BloggingAbout();<br /> }<br /> <font color="#0000ff" size="2">return</font><font size="2"> bloggingAbout;<br /> }<br /> }<br /> }<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>