ReaderWriterLock vs lock {}

Please explain what are the main differences and when should I use what. The focus is on web-threaded applications.

+48
multithreading c #
Jan 22
source share
7 answers

lock allows only one thread to execute code at a time. ReaderWriterLock can allow multiple threads to read at the same time or have exclusive write access, so it can be more efficient. If you are using the .NET 3.5 ReaderWriterLockSlim , this is even faster. Therefore, if your shared resource is read more often than writing, use ReaderWriterLockSlim . A good example of its use is a file that you read very often (for each request), and you rarely update the contents of the file. Therefore, when you read from a file, you enter a read lock so that many requests can open it for reading, and when you decide to write, you enter a write lock. Using lock in a file basically means that you can serve one request at a time.

+44
Jan 22 '10 at 11:51 on
source share

Consider using ReaderWriterLock if you have many threads that < must read data , and these threads block waiting for a lock, and you don't often need to change the data.

However, ReaderWriterLock can block a thread that has been waiting for recording for a long time.

Therefore, use only ReaderWriterLock after you have confirmed that you will get a high conflict for locking in real life, "and you have confirmed that you cannot redesign your lock design to to reduce the lock time for .

Also, think about whether you can store the shared data stored in the database in the database and let them take care of all the locks, since this much less often gives you difficult tracking errors if the database is fast enough for your application.

In some cases, you can also use the Apsnet cache to process shared data and simply delete an item from the cache when the data changes. The next reading may put a new copy in the cache.

Remember

"The best kind of lock is you don't need a lock (i.e. don't exchange data between threads).

+14
Jan 22 '10 at 11:52
source share

Monitoring and the basic "syncblock" that can be associated with any reference object - the main mechanism in C # lock - supports exceptional execution. Only one thread can have a lock. It is simple and effective.

ReaderWriterLock (or, in V3.5, the best ReaderWriterLockSlim ) provides a more complex model. Avoid if you don’t know that it will be more efficient (i.e. take performance measurements to support yourself).

The best kind of lock is a lock that you do not need (i.e. do not exchange data between threads).

+8
Jan 22 '10 at 11:55
source share

ReaderWriterLock / Slim is specifically designed to help you effectively block multiple consumption / single producer scenarios. Doing this with the lock statement is possible, but inefficient. RWL / S takes advantage by being able to aggressively spin-lock to obtain a lock. It also helps to avoid blocking convoys, a problem with the blocking operator, when a thread refuses a stream quantum, when it cannot get a lock, causing it to lag, because it will not be postponed for some time.

+6
Jan 22 '10 at 12:20
source share

ReaderWriterLock allows you to have multiple threads while holding ReadLock at the same time so that your shared data can be consumed by multiple threads at once. As soon as WriteLock is requested, ReadLocks are no longer provided, and code waiting for WriteLock is blocked until all threads with ReadLocks issue them.

WriteLock can only be held by one thread, allowing your "data updates" to appear atomic in terms of consuming parts of your code.

A lock on the other hand allows only one thread to be entered at a time, ignoring threads that are simply trying to use shared data.

ReaderWriterLockSlim is a new, more advanced version of ReaderWriterLock with better recursion support and the ability to seamlessly move a thread from a lock, which is essentially a ReadLock for WriteLock (UpgradeableReadLock).

+4
Jan 22 '10 at 12:00
source share

It is true that ReaderWriterLockSlim is FASTER than ReaderWriterLock. But the memory consumption of ReaderWriterLockSlim is absolutely outrageous. Try connecting a memory profiler and see for yourself. I would select ReaderWriterLock for the day through ReaderWriterLockSlim.

+2
May 22 '13 at 10:19
source share

I would suggest looking at http://www.albahari.com/threading/ - part three talks about ReaderWriterLockSlim (which you want to use instead of ReaderWriterLock).

+1
Jan 22 '10 at 12:01
source share



All Articles