Search code examples
c#.netreaderwriterlockslim

System.Threading.ReaderWriterLockSlim


Is it true or false of this sentence?

It is optimized for usage where writes from multiple sources are common

ReaderWriterLockSlim allows thread to lock for read or write and only one lock. But about many threads - is it allow for 2 threads to lock itself or not? I'm confused...


Solution

  • Reader/writer locks, both the slim and the fat variety, are optimized for situations where there are multiple readers but few writers.

    Both lock types allow multiple readers to access the resource simultaneously but only one writer. If a writer requests access, it is queued up until all current readers have exited, no new readers are allowed to enter during this process, and the one writer thread then has exclusive access until it releases its writer lock again.

    The main difference between slim and normal is that the latter is newer and has better performance characteristics for most common scenarios.