Guest User

Untitled

a guest
May 20th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. namespace Patterns.Threading
  2. {
  3. /// <summary>
  4. /// Singleton Design Pattern for multi-thread environment
  5. /// </summary>
  6. /// <remarks>Uses double-checked locking method - effective but expensive locking mechanism with least control.</remarks>
  7. public sealed class DoubleLockSingleton
  8. {
  9. private static readonly object _sync = new object();
  10. private static DoubleLockSingleton _instance;
  11.  
  12. private DoubleLockSingleton()
  13. {
  14. }
  15.  
  16. public static DoubleLockSingleton Instance
  17. {
  18. get
  19. {
  20. if (_instance != null)
  21. return _instance;
  22.  
  23. lock( _sync )
  24. {
  25. _instance = new DoubleLockSingleton();
  26. return _instance;
  27. }
  28. }
  29. }
  30. }
  31. }
Add Comment
Please, Sign In to add comment