Multiple threads Accessing a large dictionary object in memory - bottleneck?

Quick multithreaded question ...

I have one dictionary object in memory containing about 20,000 elements. If I have several threads trying to read from this object, do you expect this to create a bottleneck?

NET 3.5. Dictionary object will be read only

ReadOnly dictionary, so I'm not interested in reading / writing, only performance.

+4
source share
6 answers

It will not create a bottleneck, but the dictionary is not thread safe, so you cannot get the expected results. In .NET 4.0, you can use ConcurrentDictionary<TKey, TValue> for this purpose.

If this is a read dictionary, then it is probably safe to have concurrent readers, just make sure you populate this dictionary with data in a static constructor to make sure that the write does not interfere with reading, and this happens before any thread tries to read .

+5
source

If all you do is read, not write to the dictionary, then you should be fine.

In this case, thread safety is more related to mutation, so if you don't change your vocabulary in any way, you feel good.

+3
source

Depends on which locks and writes continue.

If you are not writing at the same time, I think it is safe to read without blocking, and then there is no real bottleneck. But there are no clear specifications on this.

If you write (and thus block), it depends on what else is going on.

+2
source

No bottleneck that I can see from reading, , but I have doubts about using Enumerator.

I have not proven this yet, but my instinct tells me multithreaded applications using the MIGHT dictionary enumerator cause some problems ...

+1
source

If you assume that you are initializing the dictionary, and then it remains read-only, then using a simple dictionary is great and will not have a bottleneck because it is not thread safe and therefore there is no synchronization overhead.

If you really support a read-only template, I would suggest encapsulating your dictionary in a container type class that supports read operations. Then your customers may not undermine your design intent, "wrote" to the dictionary.

+1
source

use this: System.Threading.ReaderWriterLock . create a new dictionary. block the "global" location. assign it. unlock it.

if only the data source is trying to use ReadOnly-Property

-1
source

All Articles