When does a dictionary throw an IndexOutOfRangeException on Add or ContainsKey?

后端 未结 3 1312
盖世英雄少女心
盖世英雄少女心 2020-12-31 01:19

On a busy ASP .NET website, I have a Dictionary, which acts as a cache, basically storing key/value pairs for later retrieval.

On high load, the Dictionary some time

3条回答
  •  [愿得一人]
    2020-12-31 02:03

    The documentation for Dictionary states:

    A Dictionary can support multiple readers concurrently, as long as the collection is not modified. ... To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

    If you are not synchronizing access to the Dictionary, then you might get issues like those you describe (presumably because the internal state is no longer valid). If you want to try and reproduce this in your development environment, then try creating a program that uses multiple threads to continuously read and write from a Dictionary without synchronization.

提交回复
热议问题