Home »

Should I use ReaderWriterLock instead of Monitor.Enter/Exit?

Question ListCategory: ASP.NETShould I use ReaderWriterLock instead of Monitor.Enter/Exit?
milleranthony7 author asked 1 year ago
1 Answers
jessica537 author answered 1 year ago

Maybe, but be careful. ReaderWriterLock is used to allow multiple threads toread from a data source, while still granting exclusive access to a single
writer thread. This makes sense for data access that is mostly read-only, but
there are some caveats. First, ReaderWriterLock is relatively poor performing
compared to Monitor.Enter/Exit, which offsets some of the benefits. Second,
you need to be very sure that the data structures you are accessing fully
support multithreaded read access. Finally, there is apparently a bug in the
v1.1 ReaderWriterLock that can cause starvation for writers when there are a
large number of readers.

Please login or Register to Submit Answer