TKEY的,TValue>一种解释和LT的线程安全; [英] Thread safety of a Dictionary<TKey, TValue>

查看:126
本文介绍了TKEY的,TValue>一种解释和LT的线程安全;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我初始化一个通用的字典一次,并没有进一步增加/更新/移除是允许的,是安全的有多个线程从中读取无锁(假设读者开始前字典初始化)<? / p>

有在帮助非通用哈希表,说这是多个阅读器的安全一张纸条,但我没有看到类似的不通用字典


解决方案

有关你未来的参考,该文档是在这里:



< A HREF =htt​​p://msdn.microsoft.com/en-us/library/xfhwa508.aspx> http://msdn.microsoft.com/en-us/library/xfhwa508.aspx



它说:




词典
可以支持多个读者$ b $兼任b时,只要
集合不被修改。即便如此,
通过集合进行枚举
本质上不是一个线程安全
的过程。在一个
枚举与写
访问争辩极少数情况下,收藏一定要
在整个枚举过程中锁定。
。要允许对集合进行访问
。通过多线程进行读取和
写作,你必须实现自己的
同步。



If I initialize a generic dictionary once, and no further adds/updates/removes are allowed, is it safe to have multiple threads reading from it with no locking (assuming that the dictionary is initialized before the readers are started)?

There is a note in the help for the non-generic HashTable that says that it is safe for multiple readers, but I did not see a similar not for the Generic Dictionary

解决方案

For your future reference, the documentation is here:

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

It says:

A Dictionary can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

这篇关于TKEY的,TValue&gt;一种解释和LT的线程安全;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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