ReaderWriterLockSlim vs. Monitor

后端 未结 3 1638
一向
一向 2020-12-09 06:38

I have an IDictionary implementation that internally holds n other Dictionary and distributes that insertion

相关标签:
3条回答
  • 2020-12-09 07:31

    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.

    0 讨论(0)
  • 2020-12-09 07:32

    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.

    0 讨论(0)
  • 2020-12-09 07:35

    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?

    0 讨论(0)
提交回复
热议问题