线程安全C#Singleton模式 [英] Thread Safe C# Singleton Pattern

查看:194
本文介绍了线程安全C#Singleton模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于Singleton模式的一些问题如下记载:
<一href=\"http://msdn.microsoft.com/en-us/library/ff650316.aspx\">http://msdn.microsoft.com/en-us/library/ff650316.aspx

I have some questions regarding the the singleton pattern as documented here: http://msdn.microsoft.com/en-us/library/ff650316.aspx

以下code是从文章的摘录:

The following code is an extract from the article:

using System;

public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null) 
         {
            lock (syncRoot) 
            {
               if (instance == null) 
                  instance = new Singleton();
            }
         }

         return instance;
      }
   }
}

具体地,在上面的例子中,有必要比较实例为null两次,前和锁定后?这是必要的吗?为什么不先进行锁定,并进行比较呢?

Specifically, in the above example, is there a need to compare instance to null twice, before and after the lock? Is this necessary? Why not perform the lock first and make the comparison?

有没有简化为以下问题?

Is there a problem in simplifying to the following?

   public static Singleton Instance
   {
      get 
      {
        lock (syncRoot) 
        {
           if (instance == null) 
              instance = new Singleton();
        }

         return instance;
      }
   }

时的表演锁贵?

推荐答案

执行锁的非常的昂贵相比,简单的指针检查实例!= NULL

Performing the lock is terribly expensive when compared to the simple pointer check instance != null.

您在这里看到的模式被称为双重检查锁定。其目的是避免了昂贵的锁定操作而只会需要一次(当第一次被访问的单身)。实施是这样,因为它也有保证当单初始化会有从螺纹比赛条件得到的没有错误。

The pattern you see here is called double-checked locking. Its purpose is to avoid the expensive lock operation which is only going to be needed once (when the singleton is first accessed). The implementation is such because it also has to ensure that when the singleton is initialized there will be no bugs resulting from thread race conditions.

考虑一下这种方式:裸检查(无锁定)是保证给你一个纠正仅当答案是是的,对象已经构建了可用的答案。但是,如果答案是不建呢。那你没有足够的信息,因为你真正想知道的是,它的尚未建成的并没有其他线程打算在短期内构建它的 。所以你用外检查作为一个非常快的初始测试,你开始正确的,无缺陷,但贵的过程(锁然后检查)仅如果答案是否。

Think of it this way: a bare null check (without a lock) is guaranteed to give you a correct usable answer only when that answer is "yes, the object is already constructed". But if the answer is "not constructed yet" then you don't have enough information because what you really wanted to know is that it's "not constructed yet and no other thread is intending to construct it shortly". So you use the outer check as a very quick initial test and you initiate the proper, bug-free but "expensive" procedure (lock then check) only if the answer is "no".

以上实现是大多数情况下足够好了,但在这一点上,它是一个好主意,去阅读乔恩斯基特的文章在C#也评估其他选择单身。

The above implementation is good enough for most cases, but at this point it's a good idea to go and read Jon Skeet's article on singletons in C# which also evaluates other alternatives.

这篇关于线程安全C#Singleton模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆