Use test-and-test-and-set pattern in LazyInitializer::EnsureInitialized
authorJérémie Laval <jeremie.laval@gmail.com>
Fri, 22 Oct 2010 14:15:18 +0000 (15:15 +0100)
committerJérémie Laval <jeremie.laval@gmail.com>
Fri, 22 Oct 2010 14:16:49 +0000 (15:16 +0100)
mcs/class/corlib/System.Threading/LazyInitializer.cs

index c63126ee57379b6abad59b650a9cdf7cf9bd069b..9b4ada957ac2836b4c450ea46af47b58d27b4332 100644 (file)
@@ -39,7 +39,8 @@ namespace System.Threading
                
                public static T EnsureInitialized<T> (ref T target, Func<T> initFunc) where T : class
                {
-                       Interlocked.CompareExchange (ref target, initFunc (), null);
+                       if (target == null)
+                               Interlocked.CompareExchange (ref target, initFunc (), null);
                        
                        return target;
                }