3

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, so basically if one time, the lock isn't accessible, this isn't a big deal, I just want to exit and wait for the next call.

So I've the following method's content:

if (m_lock.TryEnterUpgradeableReadLock(0)){
   ..
}

In the application, I'm currently having a deadlocks(which seems to have nothing to do with this current issue), so I paused my application to check the threads and I saw that I've more than 20 threads that are currently blocked on the if condition.

This is very weird, because the documentation is very clear about this:

If millisecondsTimeout is 0 (zero), this method checks the lock state and returns false immediately if the desired state is unavailable.

The question is simple. WHYYYYYYYYYYYYYYYYYYY? I tried to put breakpoints after the TryEnterUpgradeable, to just continue, but this doesn't work.

J4N
  • 19,480
  • 39
  • 187
  • 340
  • 2
    Assuming there's something wrong with ReaderWriterLockSlim never gets you very far. The 99.9% case is that there's something wrong with the code that uses it. You didn't post nearly enough of it, what happens when it returns *false* matters a great deal. – Hans Passant Jun 19 '15 at 12:54
  • I don't assume anything(if it was the case I would not have come here). If it returns null, nothing happens. This "if" is the only code called in a Timer event. But I'm trying to understand what could be the reason it doesn't exit from this method(this is a fact, when I pause, I've one thread which is using this lock, and thoses 20 threads waiting on this. What happens if the one having the lock is in a read lock, and this one is in an upgradeable read lock. Does it still exit directly? – J4N Jun 19 '15 at 13:56
  • One more thing, this happens ONLY if I've Visual Studio attached to the exe. If not, everything is working fine – J4N Jun 19 '15 at 13:57
  • 2
    _"The question is simple"_ -- simple to state, perhaps. But impossible to answer if you fail to provide [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that reliably reproduces the problem. – Peter Duniho Jun 20 '15 at 00:28
  • I second Peter's comment. Are you able to reproduce this problem in a very small minimal program? If so, please post that. Otherwise, your post will only help you to vent, but not to get solutions. :) – sstan Jun 20 '15 at 03:38
  • Also, just a thought. But can you go in VS, under the Debug menu, and make sure to delete ***all*** breakpoints? – sstan Jun 20 '15 at 03:42
  • I don't even manage to reproduce this while not starting it in visual Studio. I was hoping to find someone who had the same kind of issue. I will try to make a minimal example, but I'm not very confident. This happens when having a lot of disk access, and communication (remoting) which access in read this lock – J4N Jun 20 '15 at 05:26
  • Can you please show more code? Not necessary minimal code sample. Because it's definitely unclear what exactly happen. – Valery Petrov Aug 28 '15 at 06:24

0 Answers0