So the question is when should I use EnterReadLock() and EnterWriteLock() when accessing the Dictionary?
- TryGetValue. I think a ReadLock should be ok here.
- Updating the Dictionary using the Indexer e.g. _dic[existingKey] = NewValue. I think a ReadLock should be OK here.
- Add a new item using the Indexer e.g. _dic[newKey] = NewValue. I think I need a WriteLock here.
Thanks in advance!
It was already mentioned by Anton that you should use insers/updates/deletes with WriteLock and reads with ReadLock.
However you may also consider an UpgradableReadLock. Let say, you check for ContainsKey() or ContainsValue() in dictionary and then decide to update it. UpgradableReadLock would allow you efficiently upgrade to WriteLock without releasing the ReadLock and then
reacquiring the WriteLock.