ReaderWriterLockSlim is a class in Microsoft .NET framework which represents a lock that is used to manage access to a resource, allowing multiple threads for reading or exclusive access for writing.
Questions tagged [readerwriterlockslim]
79 questions
25
votes
1 answer
C# ReaderWriterLockSlim Best Practice to Avoid Recursion
I have a class using ReaderWriterLockSlim with a read method and a write method that uses the read method to retrieve the element to be modified. A quick example would be:
class FooLocker
{
ReaderWriterLockSlim locker = new…

Adam Rodger
- 3,472
- 4
- 33
- 45
22
votes
5 answers
Faster TMultiReadExclusiveWriteSynchronizer?
Is there a faster kind of TMultiReadExclusiveWriteSynchronizer out there? FastCode perhaps?
Starting with Windows Vista, Microsoft added a Slim Reader/Writer lock. It performs much better than Delphi's TMultiReadExclusiveWriteSynchronizer.…

Ian Boyd
- 246,734
- 253
- 869
- 1,219
16
votes
1 answer
Is there a ReaderWriterLockSlim equivalent that favors readers?
I've been using the ReaderWriterLockSlim for some time and it has met my needs up to this point. As I continue to fine-tune my application, I find that ReaderWriterLockSlim is slightly suboptimal for my use case.
Per documentation (and my…

Igor Pashchuk
- 2,455
- 2
- 22
- 29
14
votes
2 answers
Microsoft's remark to ReaderWriterLockSlim.IsReadLockHeld/IsWriteLockHeld and its consequences
To synchronize the access to my properties I use the ReaderWriterLockSlim class. I use the following code to access my properties in a thread-safe way.
public class SomeClass
{
public readonly ReaderWriterLockSlim SyncObj = new…

Shinja
- 343
- 1
- 8
13
votes
3 answers
Is locking access to a bool required or is it Overkill
I have a class that is designed primarily as a POCO class, with various Threads and Tasks could read its values, and only others only occasionally updating these values. This seems to be an ideal scenario for ReaderWriterLockSlim.
The question is,…

Andre
- 754
- 1
- 10
- 16
10
votes
2 answers
How to avoid blocking ReaderWriterLockSlim readers when writer is attempting to enter write lock
I'm using ReaderWriterLockSlim to guard some operations. I would like to favor readers over writers, so that when a reader holds the lock for long and a writer is attempting to acquire the write lock, further readers down the road are not blocked by…

Gabriele Giuseppini
- 1,541
- 11
- 19
9
votes
1 answer
Is there an equivalent of the lock{} statement for ReaderWriterLockSlim?
I like the shortcut in C# of lock(myLock){ /* do stuff */}. Is there an equivalent for read/write locks? (Specifically ReaderWriterLockSlim.) Right now, I use the following custom method, which I think works, but is kind of annoying because I have…

Xodarap
- 11,581
- 11
- 56
- 94
9
votes
3 answers
ReaderWriterLockSlim.EnterUpgradeableReadLock() Always A Deadlock?
I'm very familiar with ReaderWriterLockSlim but tried my hand at implementing EnterUpgradeableReadLock() recently in a class... Soon after I realized that this is almost certainly a guaranteed deadlock when 2 or more threads run the code:
Thread A…

Haney
- 32,775
- 8
- 59
- 68
8
votes
2 answers
Is it completely safe to use pattern of ReaderWriterLockSlim.EnterXXX() with consequent try-finally clause
MSDN Documentation and many examples of using ReaderWriterLockSlim class recommends using the following pattern:
cacheLock.EnterWriteLock();
try
{
//Do something
}
finally
{
cacheLock.ExitWriteLock();
}
But I'm curious if it's completely…

Sasha
- 8,537
- 4
- 49
- 76
8
votes
2 answers
Error with ReaderWriterLockSlim
I got this exception
The read lock is being released without being held.
at System.Threading.ReaderWriterLockSlim.ExitReadLock()
at .. GetBreed(String)
Below is the only place in code that accesses the lock. As you can see,…

dan
- 9,712
- 6
- 49
- 62
7
votes
2 answers
What are the real downsides of using ReaderWriterLock
We have project targeted .NET 2.0 RTM (yes, it should be .NET 2.0 RTM, we have some orthodox clients). And I'm just wondering what are the downsides of ReaderWriterLock? Why is it so bad that everyone tell "don't use it, try to use something else…

Dmitrii Lobanov
- 4,897
- 1
- 33
- 50
6
votes
2 answers
Windows Condition Variable vs. Event
We can use either the new condition variable primitive or windows event in order to synchronize threads in WinNT v6.x or later. Consider the following two approaches, we want workers to run at the same time when "go" is set in main, otherwise they…

Y.Z
- 626
- 2
- 9
- 21
6
votes
1 answer
Is it safe to mix locks and interlock operations?
I have some code which must be thread safe whose behavior resembles this:
protected long m_RunningValue
protected long m_RunningCounter
protected object m_Lock = new object();
public long RunningValue { get { return…

Chuu
- 4,301
- 2
- 28
- 54
5
votes
1 answer
Deadlock in Parallel.ForEach with ReaderWriterLockSlim
I have an interesting problem with deadlocks in an my application. There is an in-memory data store that uses a ReaderWriterLockSlim to synchronize reads and writes. One of the read methods uses Parallel.ForEach to search the store given a set of…

eakins05
- 78
- 6
5
votes
2 answers
Again double-checked locking and C#
Recently I have been refactoring some of my C# code and I found a few double-checked locking practices taking place. I didn't know it was a bad practice back then and I really want to get rid of it.
The problem is that I have a class that should be…

Ivaylo Slavov
- 8,839
- 12
- 65
- 108