Based on another answer here: 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
}
This makes assignments to the dictionary in parallel the underlying collection bar
might be resizing itself as needed.
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)
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?
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…