散列表词典<> SyncRoot上。 [英] Hashtable to Dictionary<> syncroot .

查看:84
本文介绍了散列表词典<> SyncRoot上。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哈希表有SyncRoot属性,但一般的字典没有。如果我有code,做这样的:

Hashtables have a syncroot property but generic dictionaries don't. If I have code that does this:

lock (hashtable.Syncroot)
{
....
}

我如何复制这一点,如果我删除哈希表,并更改为通用词典?

How do I replicate this if I am removing the hashtable and changing to generic dictionaries?

推荐答案

如果您是打算兼容性严格然后布莱恩是正确的。这是为了维持在一个字典的顶部您当前的语义的最佳方式。

If you are going strictly for compatability then Bryan is correct. This is the best way to maintain your current semantics on top of a Dictionary.

拓展它虽然。 SyncRoot属性没有直接加入到通用词典的原因是,它是做同步的危险的方式。这只是性能稍微比锁(本),这是非常危险的,容易出现死锁更好。这里有一对夫妇说,为什么这是不好说话的链接。

Expanding on it though. The reason the SyncRoot property was not directly added to the generic dictionary is that it's a dangerous way to do synchronization. It's only slighly better than "lock(this)" which is very dangerous and prone to deadlocks. Here are a couple of links that speak to why this is bad.

这篇关于散列表词典<> SyncRoot上。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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