使字典访问线程安全? [英] Making dictionary access thread-safe?
本文介绍了使字典访问线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案在.NET 4中,你有 ConcurrentDictionary 类。
如果你需要使用旧版本的.NET,并且想要自己写:
- 将字典换成 private 您的课程中的strong>字段
- 使用单独的
私有对象lockObject
- 在
lockObject
的每个访问字典
Possible Duplicate:
What’s the best way of implementing a thread-safe Dictionary in .NET?Hi, whats is the easiest way to make C# dictionary access thread safe? Preferablly just using lock(object) but any other ideas welcome!
解决方案In .NET 4 you have the ConcurrentDictionary class.
If you need to use an older version of .NET, and want to write it yourself:
- wrap a Dictionary as a private field in your class
- use a separate
private object lockObject
- take a lock on that
lockObject
around every access to the dictionary这篇关于使字典访问线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文