Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix #3862 - Double-Check locked patterns and consider static or Lazy<…
Browse files Browse the repository at this point in the history
…T> for Singletons (#16216)

* Fix #3862 - Double-Check locked patterns and consider static or Lazy<T> for Singletons
  • Loading branch information
WinCPP authored and stephentoub committed Feb 24, 2017
1 parent 7dd9871 commit 3e7fb77
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,11 @@ internal sealed class RuntimeBinder
{
#region Singleton Implementation

// The double checking lock, static lock initializer, and volatile instance
// field are all here to make the singleton thread-safe. Please see Richter,
// "CLR via C#" Ch. 24 for more information. This implementation was chosen
// because construction of the RuntimeBinder is expensive.

private static readonly object s_singletonLock = new object();
private static volatile RuntimeBinder s_instance;
private static readonly Lazy<RuntimeBinder> s_lazyInstance = new Lazy<RuntimeBinder>(() => new RuntimeBinder());

public static RuntimeBinder GetInstance()
{
if (s_instance == null)
{
lock (s_singletonLock)
{
if (s_instance == null)
{
s_instance = new RuntimeBinder();
}
}
}

return s_instance;
return s_lazyInstance.Value;
}

#endregion
Expand Down
Loading

0 comments on commit 3e7fb77

Please sign in to comment.