制作词典中的线程安全的? [英] Making dictionary access thread-safe?
本文介绍了制作词典中的线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/157933/whats-the-best-way-of-implementing-a-thread-safe-dictionary-in-net">What’s实施.NET?
什么是让C#词典中的线程安全的最简单的方法是什么? preferablly只用锁(对象),但任何其他的想法表示欢迎!
Hi, whats is the easiest way to make C# dictionary access thread safe? Preferablly just using lock(object) but any other ideas welcome!
推荐答案
在.NET 4中,你有的 ConcurrentDictionary 类。
In .NET 4 you have the ConcurrentDictionary class.
如果你需要使用.NET的旧版本,并希望自己写的:
If you need to use an older version of .NET, and want to write it yourself:
- 包装一个字典作为一个私人字段在你的类
- 使用一个单独的
对象lockObject
- 在就这一锁定
lockObject
周围的每一个访问字典
- wrap a Dictionary as a private field in your class
- use a separate
object lockObject
- take a lock on that
lockObject
around every access to the dictionary
这篇关于制作词典中的线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文