当该字典可能调整大小时,可以并行分配到字典吗? [英] Can I assign to a dictionary in parallel when that dictionary might resize?

查看:292
本文介绍了当该字典可能调整大小时,可以并行分配到字典吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据另一个答案: https://stackoverflow.com/a/8182978/369775

这声称我无法并行分配到字典。我从来没有遇到一个问题,分配给一个集合,并担心扩大自己的大小。

This claims that I cannot assign to a dictionary in parallel. I have never encountered an issue with assigning to a collection and worrying about its expanding its own size.

如果我从这个答案延伸,我认为它说我不能这样做例如:

If I extend from that answer I think it says that I cannot do this contrived example:

Dictionary<int,int> foo = GetFoo();
var bar = new Dictionary<int,int>();

Parallel.ForEach(foo, bat=>
    {
        bar[bat.Key] = bat.Value
    }

这使并行分配字典的底层集合 bar 可能会自己调整大小根据需要,

This makes assignments to the dictionary in parallel the underlying collection bar might be resizing itself as needed.

与Jon skeet的并行分配讨论将会发生什么:

It is very similar to what would be happening in this parallel assignment discussion from Jon skeet: Parallel Linq - Use more threads than processors (for non-CPU bound tasks)

在我看来,我不需要在上述情况下使用 ConcurrentDictionary ,但答案我链接(从一个值得注意的用户我尊重)表示否。

I seems to me that I should not need to use a ConcurrentDictionary in the situation above but the answer I linked (from a notable user I respect) indicates otherwise.

使用字典集合是安全的我描述的方式或将会失败?

Is it safe to use the Dictionary collection in the manner I described or will it fail?

有人可以提供一个代码示例,这种分配方式我失败了我正在尝试识别SLaks和Jon Skeet的答案,并同时探索分配到一个字典

Can somebody provide an code example where this sort of assigning will fail? I am trying to recognize the answers from SLaks and Jon Skeet with both explore assigning to a Dictionary in parallel.

编辑下行者可以解释我如何改进我的问题吗?如果我要求的是不公平的,请说明如何澄清。谢谢。

EDIT Can the downvoter explain how I can improve my question? If what I'm asking is unlcear please explain how I can clarify. Thanks.

推荐答案

您不能并行分配到字典,因为Dictionary类的写入说它不是线程安全的。关于为什么对讨论不重要的细节。

You cannot assign to a dictionary in parallel because the writes of the Dictionary class say it isn't thread safe. The details as to why aren't terribly important to the discussion.

如果您正在从静态字典中读取,您可以从多个线程执行此操作,因为没有阅读选项修改基础字典,但没有更新可以从多个线程安全。

If you are reading from a static dictionary you can do that from multiple threads as none of the reading options modify the underlying dictionary, but no updates are safe from multiple threads.

随意浏览逻辑,看看如果你想知道为什么并发更新可能会失败,但通常最好采用算法实现者的话,因为假设别人的逻辑是写在同时写的,因为它不支持它是更安全的。

Feel free to walk through the logic and see why concurrent updates could fail if you want to know exactly why, but typically it is best to take the algorithm implementors word, as it is safer to assume someone else's logic was written with concurrent writing in mind it does not support it.

多个写入失败的最简单的例子是第352行/ 353在这个方法的热门路径中(不太明显),并且涉及复制一个值,然后递增它。

The simplest example of multiple writes failing is lines 352/353 which are (not so obviously) in the hot path for this method and involve copying a value followed by incrementing it.

这篇关于当该字典可能调整大小时,可以并行分配到字典吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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