IndexOutOfRangeException when adding to Hashset<T>

ぃ、小莉子 提交于 2020-01-14 07:13:31

问题


I have a simple application that adds about 7 million short strings to a HashSet<string>. Occasionally I get an exception during a call to Hashset.Add(): System.Collections.Generic.HashSet`1.IncreaseCapacity(): Index was outside the bounds of the array.

It's an intermittent problem and seems related to memory, but this is on a win2k8 R2 server with 16 GB, not much else going on, most of that physical memory is available. Any ideas?


回答1:


The HashSet<T> is not thread-safe. Especially when adding items in a multi-threaded scenario and the internal capacity has to be increased, things can go out of sync.




回答2:


The instance methods on HashSet<T> are not thread-safe. In particular, when you attempt to add an element that would cause the set to exceed the bounds of the existing array in more than one thread at a time, the instance variables used to keep track of the size of the set and the last index in the set can be updated in both threads. In particular, if the last index value is updated by the second thread (with a larger value) before the first thread is finished copying the destination array, it could attempt to access an element of the local array that does not exist because the local array was allocated to only hold half as many elements as that allocated by the second thread.



来源:https://stackoverflow.com/questions/4307131/indexoutofrangeexception-when-adding-to-hashsett

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!