Questions tagged [readerwriterlockslim]

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.

79 questions
3
votes
1 answer

ReaderWriterLockSlim vs Double Lock Check pattern

EDIT: From the answers I got already, I understand that the first solution that i presented, not really "Not blocking reads", since only one thread can enter upgradable lock and write lock can not be taken before read is released... So my question,…
Alex Dn
  • 5,465
  • 7
  • 41
  • 79
2
votes
1 answer

Simplifying ReaderWriterLockSlim Syntax

I recently had to use ReaderWriterLockSlim to synchronize access to several resources that are shared between multiple threads. While doing it, I felt that using ReaderWriterLockSlim is not easy specially when you have to use it in multiple places.…
Ali Kazmi
  • 3,610
  • 6
  • 35
  • 51
2
votes
1 answer

Difference between EnterWriteLock and TryEnterWriteLock(Timeout.Infinite)

i'm a bit confused on the differences between EnterWriteLock() and TryEnterWriteLock() with Timeout.Infinite as parameter of ReaderWriterLockSlim. What is the point of giving an infinite timeout for entering the lock instead of using directly…
LMio
  • 126
  • 1
  • 9
2
votes
2 answers

Why using ReaderWriterLockSlim doesn't make my Dictionary thread safe?

I wrote a small piece of code that rapidly read and write to a dictionary from multiple threads. I used ReaderWriterLockSlim to protect the code and still received an exception for allegedly trying to add duplicate keys. ReaderWriterLockSlim _lock =…
HuBeZa
  • 4,715
  • 3
  • 36
  • 58
2
votes
0 answers

ReaderWriterLock that allows starvation

The following code does not complete, because second reader (tRead2) cannot acquire lock. ReaderWriterLockSlim rw = new ReaderWriterLockSlim(); var tRead1 = Task.Run ( () => rw.EnterReadLock() ); var tWrite = Task.Run ( …
user3284063
  • 665
  • 5
  • 20
2
votes
1 answer

ReaderWriterLockSlim and timer

I have a frustration with ReaderWriterLockSlim and delaying ExitWriteLock. Why is the WriteLock released in timers callback? var _lock = new ReaderWriterLockSlim(); _lock.EnterWriteLock(); Assert.AreEqual(true, _lock.IsWriteLockHeld); //…
Andrey Ershov
  • 1,773
  • 1
  • 16
  • 26
2
votes
0 answers

ReaderWriterLockSlim gets locked forever

Good time, all. My problem is the following: numerous threads are waiting for an event to acquire read lock, one thread is waiting for an event to obtain write lock. Lock is not held by any thread at that moment. 0:173> !do 0x0000000001c679f8 Name: …
NoRegsz
  • 21
  • 2
2
votes
2 answers

ReaderWriterLockSlim (not so slim) replacement, recursive, with upgradeable read?

Somehow we got a LOT of ReaderWriterLockSlim in our code. Each of them takes 6K memory, so this has become a big issue. As a quick fix, I'm looking for a less memory-hungry replacement. I'm trying a Joe Duffy's RW-lock, but it's not upgradeable and…
Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91
2
votes
3 answers

c# ReaderWriterLockSlim corrupt after interrupt thread that invokes ExitReadLock

I encountered a scenario where ReaderWriterLockSlim seems to get broken after a series of legal actions. The flow is: Thread 1 takes writer lock1 Thread 2 tries to take reader lock1 - blocks Thread 2 is interrupted, and calls lock1.ExitReadLock…
omamluk
  • 23
  • 5
2
votes
1 answer

IDisposable and ReaderWriterLockSlim

I have a class MyClass. This class have a field: public ReaderWriterLockSlim rw; (public for an easier example code). Many threads can read data from MyClass using rw.EnterReadLock etc. Also I have implemented IDisposable interface: private void…
apocalypse
  • 5,764
  • 9
  • 47
  • 95
1
vote
2 answers

Thread-safe enumeration of shared memory that can be updated or deleted

I have a shared object between threads that is used to hold file state information. The object that holds the information is this class: /// /// A synchronized dictionary class. /// Uses ReaderWriterLockSlim to handle locking. The…
mslot
  • 4,959
  • 9
  • 44
  • 76
1
vote
2 answers

Does this ReaderWriterLockSlim locking pattern look right?

I've been using ReaderWriterLockSlim to synchronize reads/writes and it has worked quite well so far. I have a collection of objects and read/write locks are maintained in a Dictionary. I have a scenario where I need…
Igor Pashchuk
  • 2,455
  • 2
  • 22
  • 29
1
vote
1 answer

LockRecursionException in multiple threaded function

I dont know how to describe it but I'm getting exception that schouldn't have a place when a code is good written. This exception is about issue with ReaderWriterLockSlim and it's LockRecursionException; it is appearing at…
1
vote
1 answer

Why does ReaderWriterLockSlim call Sleep() inside of EnterReadLock()?

I'm trying to implementing a threadsafe resource pool that is fast to access and only gets occasional updates. For that I use the ReaderWriterLockSlim with an extension method to enter and exit the read lock. While profiling the thread utilization…
1
vote
1 answer

ReaderWriterLockSlim with LockRecursionPolicy.SupportsRecursion vs lock

I am confused about the warning for ReaderWriterLockSlim SupportsRecursion From MSDN By default, new instances of ReaderWriterLockSlim are created with the LockRecursionPolicy.NoRecursion flag and do not allow recursion. This default policy is…
kofifus
  • 17,260
  • 17
  • 99
  • 173