多级ConcurrentDictionary还是线程安全的吗?

发布于 2024-10-16 17:44:22 字数 551 浏览 8 评论 0 原文

我有这样定义的四级数据结构:

Dictionary<Type1, Dictionary<Type2, Dictionary<Type3, List<Type4>>>>

整个事物被封装在一个类中,该类也保持线程安全。目前,它只是在读取/操作数据时锁定整个集合(读取比写入更常见几个数量级)。

我正在考虑用 ConcurrentDictionary 替换 Dictionary List 以及 < code>ConcurrentBag(其项目不必订购)。

如果这样做,我可以消除锁并确保并发集合能够正确完成其工作吗?

I have four level data structure defined like this:

Dictionary<Type1, Dictionary<Type2, Dictionary<Type3, List<Type4>>>>

The whole thing is encapsulated in a class which also maintains thread-safety. Currently it just locks whole collection while it reads/manipulates the data (reading is by orders of magnitude more common than writing).

I was thinking of replacing the Dictionary with ConcurrentDictionary and List with ConcurrentBag (its items don't have to be ordered).

If I do so, can I just eliminate the locks and be sure the concurrent collections will do their job correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只等公子 2024-10-23 17:44:22

我迟了将近一年才提出这个问题..但以防万一有人发现自己处于与 Matěj Zábský 类似的位置,问问自己:

你可以使用 Dictionary, ListDictionary, ListDictionary, List<输入4>> 代替?

使用起来要容易得多,并且考虑到哈希表(即字典)是 O(1) 数据结构,具有相当大的常量组件(如果您转向 ConcurrentDictionary 则更是如此),它可能会执行速度也更快。它还会使用更少的内存,并且转换为 ConcurrentDictionary 也非常简单。

当然,如果您需要枚举给定 Type1 键的所有给定 Type2,则嵌套字典可能是最佳选择。但这是一个要求吗?

I'm nearly a year late to the question.. but just in case anyone finds themselves in a similar position to Matěj Zábský, ask yourself:

Can you use a Dictionary<Tuple<Type1, Type2, Type3>, List<Type4>> instead?

Considerably easier to work with, and considering that hash tables (ie Dictionaries) are O(1) data structures with a somewhat hefty constant component (even more so if you move to a ConcurrentDictionary) it'd likely perform faster too. It'd also use less memory, and be pretty trivial to convert to a ConcurrentDictionary.

Of course if you need to enumerate all of a given Type2 for a given Type1 key, the nested dictionaries is possibly the way to go. But is that a requirement?

著墨染雨君画夕 2024-10-23 17:44:22

并发集合将防止数据损坏和崩溃,但代码在语义上不会与当前代码等效。例如,如果您迭代并发词典之一,某些项目可能属于不同的更新

从返回的枚举器
字典可以安全地同时使用
与读取和写入
字典,但它没有
代表某个时刻的快照
字典。曝光的内容
通过枚举器可能包含
对字典进行的修改
调用 GetEnumerator 后。

如果您想保持现在的确切行为,同时节省锁定成本,您可能需要使用 ReaderWriterLockSlim 特别适合读取多于写入的情况。

The concurrent collections will prevent data corruption and crashes, but the code won't be semantically equivalent to your current one. For example, if you iterate one of the concurrent dictionaries, some of the items may belong to different updates:

The enumerator returned from the
dictionary is safe to use concurrently
with reads and writes to the
dictionary, however it does not
represent a moment-in-time snapshot of
the dictionary. The contents exposed
through the enumerator may contain
modifications made to the dictionary
after GetEnumerator was called.

If you want to maintain the exact behavior you have now, yet save on the cost of locking, you may want to lock with ReaderWriterLockSlim which is especially suited for cases with more reads than writes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文