I have an IDictionary
implementation that internally holds n other Dictionary
and distributes that insertion
For write-only load the Monitor is cheaper than ReaderWriterLockSlim, however, if you simulate read + write load where read is much greater than write, then ReaderWriterLockSlim should out perform Monitor.
I'm no guru, but my guess is that RWLS is more geared towards heavy contention (e.g., hundreds of threads) whereas Monitor
is more attuned towards those one-off synchronization issues.
Personally I use a TimerLock
class that uses the Monitor.TryEnter
with a timeout parameter.
How do you know what caused the bad performance? You can't go guessing it, the only way is to do some kind of profiling.
How do you handle locking for the parent collection or is it constant?
Maybe you need to add some debug output and see what really happens?