字典用锁或Concurency字典? [英] Dictionary with lock or Concurency Dictionary?

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

问题描述

这是pferred在多线程应用程序$ P $:  字典,提供锁定对象 要么  并发字典

Which is preferred in a multi-threaded application: Dictionary with lock object or Concurrency Dictionary

这是有效的,我为什么要使用一个或其他?

Which is efficient and why should I use one or the other?

编辑1:存储的GUID键和布尔作为价值

edit 1: Storing Guid as key and bool as value.

编辑2:2个以上的工作线程,一个UI线程

edit 2: more than 2 worker threads and one UI thread.

推荐答案

请仔细阅读有关的 ConcurrentDictionary 。它有一些不明显的特点。

Read carefully about ConcurrentDictionary. It has some unobvious features.

下面是其中一些:

  • 如果两个线程调用 AddOrUpdate 有不保证其工厂代表将被调用,甚至不能保证,如果一个工厂代表将产生一些项目,这个项目将是存储在字典中。
  • 的GetEnumerator 获得枚举器调用的不可以的快照,可以枚举过程中被修改(即不引起任何异常)。
  • 属性对应的集合的快照,并可能与实际的词典状态。
  • If two threads call AddOrUpdate there's no guarantees about which of factory delegates will be called and even no guarantee that if a factory delegate will produce some item that this item will be stored in dictionary.
  • Enumerator obtained by GetEnumerator call is not a snapshot and may be modified during enumeration (that doesn't cause any exceptions).
  • Keys and Values properties are snapshots of corresponding collections and may not correspond to actual dictionary state.
  • etc.

所以,请阅读 ConcurrentDictionary 再次决定,如果这种行为是你所需要的。

So please read about ConcurrentDictionary again and decide if this behavior is what you need.

希望这有助于!

这篇关于字典用锁或Concurency字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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