C#中着名的双重检查锁定技术

rom*_*man 6 c# singleton thread-safety

我在一本书中看到了这个单身人士的推荐(部分代码附件):

public static Singleton GetSingleton() 
{
    if (s_value != null) 
        return s_value;
    Monitor.Enter(s_lock); 
    if (s_value == null) 
    {
        Singleton temp = new Singleton();
        Interlocked.Exchange(ref s_value, temp);
    }
    Monitor.Exit(s_lock);
    return s_value;
}
Run Code Online (Sandbox Code Playgroud)

我们在第二个if语句块中添加两行代码,而不是只写:

s_value = new Singleton();
Run Code Online (Sandbox Code Playgroud)

这应该处理第二个线程进入方法并查找s_value != null但未初始化的情况.


我的问题是,我们可以只在第二个if块写入:

{    
    Singleton temp = new Singleton();
    s_value = temp;  // instead of Interlocked.Exchange(ref s_value, temp);    
}
Run Code Online (Sandbox Code Playgroud)

所以现在的功能是:

public static Singleton GetSingleton() 
{      
    if (s_value != null)
        return s_value;

    Monitor.Enter(s_lock);   
    if (s_value == null)   
    { 
        Singleton temp = new Singleton();    
        s_value = temp; 
    }   
    Monitor.Exit(s_lock);   
    return s_value; 
} 
Run Code Online (Sandbox Code Playgroud)

我猜不是,因为他们不使用它.

有没有人有什么建议?


svalue可能包含未初始化的?可以在temp完全初始化之后构建svalue(可能我错了).如果我错了你可以指出一个例子它是错的吗?编译器可能会生成不同的代码吗?


Jer*_*Gee 7

我发布这不是一个真正的答案,但作为一个旁白:如果你使用.NET 4,你真的应该考虑Lazy<T>单例模式:

http://geekswithblogs.net/BlackRabbitCoder/archive/2010/05/19/c-system.lazylttgt-and-the-singleton-design-pattern.aspx

public class LazySingleton3
{
   // static holder for instance, need to use lambda to construct since constructor private
   private static readonly Lazy<LazySingleton3> _instance
       = new Lazy<LazySingleton3>(() => new LazySingleton3());

   // private to prevent direct instantiation.
   private LazySingleton3()
   {
   }

   // accessor for instance
   public static LazySingleton3 Instance
   {
       get
       {
           return _instance.Value;
       }
   }
Run Code Online (Sandbox Code Playgroud)

}

线程安全,易于阅读和明显:什么不喜欢?

  • 我不确定问题是否要求完美(懒惰)单例模式,或者想要理解为什么在"Interlocked"类的帮助下完成赋值. (2认同)