Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Search Your Question

Showing posts with label Threading. Show all posts
Showing posts with label Threading. Show all posts

Saturday, April 26, 2008

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

Maybe, carefully.

ReaderWriterLock is used to allow multiple threads to read 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.