ReaderWriterLock vs lock
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, lock (which uses Monitor internally) provides exclusive access — only one thread can enter the critical section at a time, whether reading or writing. ReaderWriterLockSlim allows multiple concurrent readers but only one writer, and writers have exclusive access (no readers while writing). The choice depends on your read-to-write ratio: lock is simpler and faster for balanced or write-heavy workloads, while ReaderWriterLockSlim provides better throughput when reads vastly outnumber writes. The older ReaderWriterLock class should not be used — always use ReaderWriterLockSlim.
Using lock (Monitor)
lock is syntactic sugar for Monitor.Enter/Exit. It serializes all access — even concurrent reads block each other. This is correct and simple but limits throughput when many threads read simultaneously.
Using ReaderWriterLockSlim
Multiple threads can hold the read lock simultaneously. When a thread enters the write lock, it waits for all readers to exit and blocks new readers until the write completes.
Upgradeable Read Lock
EnterUpgradeableReadLock allows one thread to read and conditionally upgrade to a write lock without releasing the read lock first. Only one upgradeable lock can be held at a time (to prevent deadlocks), but other readers can proceed concurrently.
Performance Comparison
| Feature | lock | ReaderWriterLockSlim |
| Concurrent readers | No | Yes |
| Lock acquisition overhead | ~20ns | ~40-60ns |
| Code complexity | Simple | Moderate (try/finally) |
| Upgradeable locks | No | Yes |
| Supports recursion | Yes (reentrant) | Optional (LockRecursionPolicy) |
| Best for | Write-heavy, short critical sections | Read-heavy, longer critical sections |
Using ConcurrentDictionary as an Alternative
For simple cache scenarios, ConcurrentDictionary is often the best choice — it handles synchronization internally using fine-grained locking and is optimized for concurrent access.
Common Pitfalls
- Using
ReaderWriterLockinstead ofReaderWriterLockSlim: The olderReaderWriterLockclass has significantly worse performance and is prone to deadlocks with recursive locking. Always useReaderWriterLockSlim, which was introduced in .NET 3.5 as its replacement. - Forgetting try/finally with
ReaderWriterLockSlim: Unlikelock(which guaranteesMonitor.Exitin all cases),ReaderWriterLockSlimrequires explicitExitcalls. If an exception occurs betweenEnterandExit, the lock is never released, causing all subsequent threads to hang. Always usetry/finally. - Using
ReaderWriterLockSlimfor short critical sections: If the code inside the lock runs in microseconds, the overhead of acquiring a reader-writer lock (~40ns) outweighs the benefit of concurrent reads. A simplelock(~20ns) performs better for very short critical sections. - Enabling recursion without need:
new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion)adds significant overhead. The defaultNoRecursionpolicy is faster and prevents accidental recursive locking bugs. Only enable recursion if your code genuinely requires it. - Not disposing
ReaderWriterLockSlim:ReaderWriterLockSlimimplementsIDisposableand holds kernel wait handles internally. Failing to dispose it causes resource leaks in long-running applications. Dispose it when the containing object is no longer needed.
Summary
lockprovides exclusive access with low overhead — best for write-heavy or short critical sectionsReaderWriterLockSlimallows concurrent readers — best when reads outnumber writes by 10:1 or more- Always use
ReaderWriterLockSlim(not the olderReaderWriterLock) - Wrap
EnterReadLock/EnterWriteLockintry/finallyto ensure locks are released - Consider
ConcurrentDictionaryfor simple key-value cache scenarios - Benchmark your specific workload — the theoretical advantage of reader-writer locks does not always translate to real-world speedups
Related reading
- ReaderWriterLock vs lock
- ReadFile doesn't work asynchronously on Win7 and Win2k8
- Reading asynchronous pipe - loosing data
- Reading asynchronously from stdin with Qt
- Reading a C/C data structure in C from a byte array
- Reading Excel files from C
- Recover an Asynch ThreadPoolTaskexecutor after server crashed/shut down
- Recursive Lock Mutex vs Non-Recursive Lock Mutex

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.