问题描述
哈希表有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上。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!