1

I have following code:

    private static readonly ReaderWriterLockSlim Locker = new ReaderWriterLockSlim();

    private void TryGetReadLock()
    {
        if (!Locker.IsWriteLockHeld && !Locker.IsReadLockHeld)
            Locker.TryEnterReadLock(WaitTimeout);
    }

Locker.TryEnterReadLock times out. If I stop the debugger before it times out, both Locker.IsWriteLockHeld and Locker.IsReadLockHeld are false.

Question: why would TryEnterReadLock wait in this particular case?

[EDIT] Indeed, as indicated in the comments I have lock between checking your if condition and entering the if block. So, I have tried to remove the if and leave just Locker.TryEnterReadLock(WaitTimeout);, but I receive the following error:

Additional information: A read lock may not be acquired with the write lock held in this mode.

Indeed some other thread has a write lock (Locker.IsWriteLockHeld = true).

What I want is obtain classic lock locking when one thread has the write lock, that is have the threads wait until one it finishes, but I cannot find a way to do it.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • What is the value of `WaitTimeout`? Sounds like you have a deadlock. – Matthew Watson Sep 27 '17 at 12:23
  • 3
    This is definitely not your real code, because the code you posted does enter the lock. Please post an MVCE. The only way that `TryEnterReadLock` would block is if another thread got the lock between checking your `if` condition and entering the `if` block. – Rotem Sep 27 '17 at 12:23
  • @MatthewWatson - WaitTimeout is 30 seconds, but I think it is a deadlock too. But I do not understand how it is produced since the locker indicates that no other thread has the any lock held and actually I cannot find any other worker thread within Threads window. – Alexei - check Codidact Sep 27 '17 at 12:29
  • 1
    Please create a [mcve]. Your first attempt had not enough code, the second attempt has too much irrelevant code. You should be able to reproduce this in about 10 lines, which is easier for you to troubleshoot and for us to answer. – CodeCaster Sep 27 '17 at 12:30
  • 3
    `the locker indicates that no other thread has the any lock held` - Well, be aware that your code has a race condition, since the lock could become held inbetween the `if (!Locker.IsWriteLockHeld && !Locker.IsReadLockHeld)` succeeding and the subsequent `Locker.TryEnterReadLock(WaitTimeout);` – Matthew Watson Sep 27 '17 at 12:34
  • @MatthewWatson - you are right. However, I am not able to figure out how to use `ReaderWriterLockSlim ` in this scenario. Clearly I cannot check lock state since it is not thread safe. But, I also receive an exception when I try to directly lock. – Alexei - check Codidact Sep 27 '17 at 12:45
  • 1
    IsRead/WriteLockHeld just don't do what you think it does. It does *not* tell you that another thread has the lock, it only tells you if the current thread has the lock. So that just completely doesn't help. And is a nasty race bug. Delete that to get ahead, you'll have to focus on another thread holding the lock entirely too long. – Hans Passant Sep 27 '17 at 13:31
  • Hans FTW. Also MSDN notes for these two properties `This property is intended for use in asserts or for other debugging purposes. Do not use it to control the flow of program execution.` https://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.isreadlockheld(v=vs.110).aspx – Rotem Sep 27 '17 at 13:50

0 Answers0