Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77bf9eb

Browse files
committed
comments for thread-safe singleton
1 parent de97c30 commit 77bf9eb

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

CreationalPatterns/Singleton/ChocolateBoiler_ThreadSafe.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public sealed class ChocolateBuilder_ThreadSafe
1212
public bool Boiled { get; set; }
1313

1414
// singleton instance
15+
// the volatile keyword ensures that multiple threads handle the singleton instance variable correctly
1516
private static volatile ChocolateBuilder_ThreadSafe instance;
1617
private static object syncLock = new object();
1718

@@ -25,10 +26,12 @@ public static ChocolateBuilder_ThreadSafe Instance
2526
{
2627
get
2728
{
29+
// Check for an instance and if there isn't one, enter a locked block
2830
if (instance == null)
2931
{
3032
lock (syncLock)
3133
{
34+
// Once in the block, check again and if still null
3235
if(instance == null)
3336
{
3437
instance = new ChocolateBuilder_ThreadSafe();

0 commit comments

Comments
 (0)