Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Threads Accessing Large Dictionary object in memory - bottleneck?

Quick multithreading question...

I have a single dictionary object in memory containing about 20,000 items. If I have multiple threads attempting to read from this object, would you expect this to create a bottleneck?

NET 3.5 . Dictionary object would be read only

Dictionary is ReadOnly so I'm not concerned about read/writes, only performance.

like image 921
Mark 909 Avatar asked Dec 22 '22 20:12

Mark 909


1 Answers

It won't create a bottleneck but a dictionary is not thread safe so you might not get the expected results. In .NET 4.0 you could use ConcurrentDictionary<TKey, TValue> for this purpose.

If it's a readonly dictionary then it's probably safe to have concurrent readers, just make sure that you fill this dictionary with data in a static constructor to make sure that writing doesn't interfere with reading and it takes place before any thread tries to read.

like image 78
Darin Dimitrov Avatar answered Feb 11 '23 23:02

Darin Dimitrov