使字典访问线程安全? [英] Making dictionary access thread-safe?

查看:119
本文介绍了使字典访问线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

HiㄧHi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi????????????????????????????????最好只使用lock(object),但是任何其他想法都欢迎!

在.NET 4中,你有 ConcurrentDictionary 类。



如果你需要使用旧版本的.NET,并且想要自己写:




  • 将字典换成 private 字段

  • 使用单独的私有对象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屋!

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