多线程安全单例类问题 [英] Multi-threaded safe singleton class question

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

问题描述




我喜欢MSDN上提供的单例类的C#实现。我的问题

是正确或正确的做法修改该类使得

类如果在调用
GetInstance?

公共静态Singleton GetInstance(字符串参数)

{

//使用哈希表或其他一些类型来保存实例 - 参数

将是关键

//如果密钥存在且实例不为null则返回

实例,否则创建新实例然后添加到哈希表

}


非常感谢!

John

Hi,

I like the C# implemention of singleton class presented at MSDN. My question
is that is it correct or right thing to do to modify that class to make the
class will return the same instance if for the same parameter when calling
GetInstance?

public static Singleton GetInstance(string parameter)
{
//using a hashtable or some other type to hold the instance - parameter
will be the key
//if the key exists and the instance is not null then return the
instance, otherwise create new instance and then add to hashtable
}

Thanks a lot!
John

推荐答案

我们正在为我们的许多单身人士做这件事。课程,它

工作得很好。它是否是线程安全的完全是另一个问题。

我会把它留给Jon Skeet或其他更熟悉

线程的人。

We''re doing exactly this for many of our "singleton" classes, and it
works just fine. Whether it''s thread-safe is another question entirely.
I''ll leave that to Jon Skeet or someone else more familiar with
threading.


John,


在这种情况下,从某种意义上说,它不是真正的单身人士/>
多个Singleton实例。


为了保证线程安全,你应该将哈希表传递给

静态同步方法Hashtable类。这将为你传递一个

包装器,它将在Hashtable上执行操作时提供线程安全性。
$ / b

希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" John Lee" <乔*** @ newsgroup.nospam>在消息中写道

news:或************** @ TK2MSFTNGP09.phx.gbl ...
John,

In this case, it isn''t really a singleton, in the sense that you have
more than one Singleton instance.

In order to guarantee thread safety, you should pass the hashtable to
the static Synchronized method on the Hashtable class. This will pass a
wrapper to you which will provide thread safety when performing operations
(read and write) on the Hashtable.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Lee" <jo***@newsgroup.nospam> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...

我喜欢MSDN上提供的单例类的C#实现。我的
问题是修改那个类是正确的还是正确的
如果对于相同的参数
,当调用GetInstance时,类将返回相同的实例?

public static Singleton GetInstance(字符串参数)
//
//使用哈希表或其他类型来保存实例 - 参数
将是关键
//如果密钥存在且实例不为null则返回
实例,否则创建新实例然后添加到哈希表
}

非常感谢!
John
Hi,

I like the C# implemention of singleton class presented at MSDN. My
question is that is it correct or right thing to do to modify that class
to make the class will return the same instance if for the same parameter
when calling GetInstance?

public static Singleton GetInstance(string parameter)
{
//using a hashtable or some other type to hold the instance - parameter
will be the key
//if the key exists and the instance is not null then return the
instance, otherwise create new instance and then add to hashtable
}

Thanks a lot!
John



John Lee< jo *** @ newsgroup.nospam>写道:
John Lee <jo***@newsgroup.nospam> wrote:
我喜欢在MSDN上呈现的单例类的C#实现。我的问题
是正确或正确的做法是修改该类以使
类在调用
GetInstance时为相同的参数返回相同的实例?

public static Singleton GetInstance(字符串参数)
//
//使用哈希表或其他类型来保存实例 - 参数
将是关键
//如果密钥存在且实例不为null则返回
实例,否则创建新实例然后添加到哈希表
}
I like the C# implemention of singleton class presented at MSDN. My question
is that is it correct or right thing to do to modify that class to make the
class will return the same instance if for the same parameter when calling
GetInstance?

public static Singleton GetInstance(string parameter)
{
//using a hashtable or some other type to hold the instance - parameter
will be the key
//if the key exists and the instance is not null then return the
instance, otherwise create new instance and then add to hashtable
}




As Nicholas说,这不是一个单身人士 - 它是一个工厂

模式实现。


为了线程安全,你应该拿出锁:


静态对象mapLock = new object();

public static Singleton GetInstance(字符串参数)

{

lock(mapLock)

{

//检查是否存在,创建新实例等并将其返回

} < br $>
}


不要尝试双重检查锁定 - 除非你真的,真的*

自信你做得对,并且有专家来验证它,最好通过与更多的专家交谈,你可能会有一个问题。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要也寄给我



As Nicholas said, that''s not really a singleton - it''s a factory
pattern implementation.

For thread safety, you should take out a lock:

static object mapLock = new object();

public static Singleton GetInstance (string parameter)
{
lock (mapLock)
{
// Check for existence, create new instance etc and return it
}
}

Don''t try double-checked locking - unless you''re *really, really*
confident that you''re doing it right, and have got an expert to
validate it, preferrably by talking with even more experts, you may
well have a problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于多线程安全单例类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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