C#中着名的双重检查锁定技术
发布时间:2020-12-15 04:09:53 所属栏目:百科 来源:网络整理
导读:我在一本书中看到了这个单身人士的推荐(部分代码附件): 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_v
我在一本书中看到了这个单身人士的推荐(部分代码附件):
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; } 我们在第二个if语句块中添加两行代码,而不是只写: s_value = new Singleton(); 这应该处理第二个线程进入方法并找到s_value!= null但未初始化的情况. 我的问题是,我们可以只在第二个if块写入: { Singleton temp = new Singleton(); s_value = temp; // instead of Interlocked.Exchange(ref s_value,temp); } 所以现在的功能是: 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; } 我猜不是,因为他们不使用它. 有没有人有什么建议? svalue可能包含未初始化的? 解决方法
我会发布这不是一个真正的答案,但作为一个旁白:如果你使用.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; } } } 线程安全,易于阅读和明显:什么不喜欢? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |