File tree Expand file tree Collapse file tree 1 file changed +3
-0
lines changed
CreationalPatterns/Singleton Expand file tree Collapse file tree 1 file changed +3
-0
lines changed Original file line number Diff line number Diff 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 ( ) ;
You can’t perform that action at this time.
0 commit comments