Alex Coronas
Alex Coronas

Reputation: 446

How do bulk actions affect a ConcurrentDictionary?

I'm confused about how Concurrent Dictionaries lock their resources.

For example, if I run a method that iterates over every item in the Dictionary and edits its value in a thread and I try to read the value of a key from another thread:

Will the second thread be accessing a snapshot of the Dictionary? If not, will it access the updated entry if that one has already been updated?

Upvotes: 2

Views: 336

Answers (2)

S.N
S.N

Reputation: 5140

Concurrent Dictionaries are thread-safe that can be accessed by multiple threads concurrently. Read operations on the dictionary are performed in a lock-free manner whereas write is protected with lock. For implementation details, please check ConcurrentDictionary.

Upvotes: 2

simon-pearson
simon-pearson

Reputation: 1970

The second thread will be accessing the updated dictionary. All a ConcurrentDictionary does is provide put a lock on the collection on any add or remove operations. Read operations are unaffected.

Source

Upvotes: 0

Related Questions