I have a dictionary only supports add
and modify
operations and can be concurrently operated, but always for different keys. Keys are int and values are a reference type. Also modify means change some properties of a value.
My questions are:
Thanks!
Do I need to use ConcurrentDictionary in this scenario? If needed, how does it help?
Yes, the standard dictionary will not behave correctly if more than one thread adds or removes entries at the same time. (although it is safe for multiple threads to read from it at the same time if no others are modifying it).
If concurrent modification can happen on the same key, will ConcurrentDictionary help to ensure thread safety? My understanding is no, is that correct?
If you are asking "Will the concurrent dictionary prevent multiple threads from accessing the values inside the dictionary at the same time?", then no, it will not.
If you want to prevent multiple threads from accessing the same value at the same time you will need to use some sort of concurrency control, such as locking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With