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
5
votes
2 answers

Is ReaderWriterLockSlim resistant to ThreadAbortException?

I would like to check whether following code is resistant against ThreadAbortException and will not lead into orphan lock. If it is not, what is the best pattern to avoid orphan locks here? ReaderWriterLockSlim _lock = new…
user2126375
  • 1,594
  • 12
  • 29
4
votes
0 answers

C# Handle two methods with same lock differently

I have a class with two methods (one instance): public class Example { public void Connect() { // do stuff } public void DoSomething() { if (someCondition) Connect(); // do stuff } } It…
Creepin
  • 482
  • 4
  • 20
4
votes
1 answer

Mysterious deadlock corruption with ReaderWriterLockSlim

I wrote a fairly trivial wrapper around ReaderWriterLockSlim: class SimpleReaderWriterLock { private class Guard : IDisposable { public Guard(Action action) { _Action = action; } public void…
Miral
  • 12,637
  • 4
  • 53
  • 93
4
votes
2 answers

Determining which method is holding a ReaderWriterLockSlim WriteLock

Currently I am analyzing a dump with WinDbg. I ran following commands (following Tess' incredible walkthrough): ~* e !clrstack Which listed me all stacks of all threads. There are 300 running threads with more or less the same stack, so I am just…
user57508
4
votes
4 answers

ReaderWriterLockSection: A bad idea?

In writing some threaded code, I've been using the ReaderWriterLockSlim class to handle synchronized access to variables. Doing this, I noticed I was always writing try-finally blocks, the same for each method and property. Seeing an opportunity to…
Paul Turner
  • 38,949
  • 15
  • 102
  • 166
4
votes
5 answers

How to know who owns a ReaderWriterLockSlim?

I'm writting an application that does extensive use of multithreading. Some of the threads share an observablecollection using a ReaderWriterLockSlim. I'm having from time to time a deadlock and I need to know which is the thread holding the lock at…
Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
3
votes
2 answers

ReaderWriteLockSlim or Lock

I m using ConcurrentBag to store object in run time. At some point I need to empty the bag and store the bag content to a list. This is what i do: IList list = new List(); lock (bag) { T pixel; …
DarthVader
  • 52,984
  • 76
  • 209
  • 300
3
votes
5 answers

Is it thread-safe to iterate over an immutable copy of Dictionary values if this copy was made under a lock?

Is the below code thread-safe? I need to call an async method on every service, therefore I cannot keep the foreach loop under the lock. But would it be thread-safe to copy all the values from the _dictionary to an ImmutableList under the lock, exit…
yaskovdev
  • 1,213
  • 1
  • 12
  • 22
3
votes
4 answers

upgradable reader lock in c#

I have a dictionary that is shared among number of threads. Every thread reads specific value from the dictionary according to a given key, but - if the key does not exist in the dictionary the thread need to add it to the dictionary. To solve the…
ET.
  • 1,899
  • 2
  • 18
  • 28
3
votes
3 answers

Is ReaderWriterLockSlim the right choice?

I'm writing a global error handler/logger for applications running in Windows Azure. When an error occurs in the application, a number of operations are performed that need to happen atomically. I need to prevent an error from being logged until the…
Jonathan Carter
  • 1,029
  • 1
  • 9
  • 17
3
votes
1 answer

ReaderWriterLockSlim LockRecursionPolicy.SupportsRecursion DeadLock

I have a Queue of Actions for Database Writing which manged by one dedicated Thread. And I have lots of threads that reads from Database whenever they want. I am using ReaderWriterLockSlim for READ/WRITE access control. My question is - Why…
Pavel Durov
  • 1,287
  • 2
  • 13
  • 28
3
votes
0 answers

ReaderWriterLockSlim.TryEnterUpgradeableReadLock(0) is blocking

In my .Net application, I've a place where I declare a ReaderWriterLockSlim: private readonly ReaderWriterLockSlim m_lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); At some other place, I refresh some value every 100ms,…
J4N
  • 19,480
  • 39
  • 187
  • 340
3
votes
1 answer

ConcurrentBag vs Custom Thread Safe List

I have a .NET 4.5 Single Instance WCF service which maintains a collection of items in a list which will have simultaneous concurrent readers and writers, but with far more readers than writers. I am currently deciding on whether to use the BCL…
3
votes
2 answers

Is ReaderWriterLockSlim.EnterUpgradeableReadLock() essentially the same as Monitor.Enter()?

So I have a situation where I may have many, many reads and only the occasional write to a resource shared between multiple threads. A long time ago I read about ReaderWriterLock, and have read about ReaderWriterGate which attempts to mitigate the…
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
3
votes
1 answer

Does the MSDN example usage of ReaderWriterLockSlim contain deadlock risk?

I'm using a ReaderWriterLockSlim to protect access to the cache on my ASP.NET application. MSDN has examples of using the lock. However this article http://www.nobletech.co.uk/Articles/ReaderWriterLockMgr.aspx has me worried about deadlocks. Is this…
Andrew Davey
  • 5,441
  • 3
  • 43
  • 57