views:

215

answers:

1

Hi, due to performance problems I have replaced RWL with RWLSlim but I am experiencing troubles caused by previously (with RWL) accepted statements.

As you can see, sometimes methodA calls another one which have inside ReadLock. The second method is also called from different places, so not always there is lock collision. Previously, AcquiringRead lock doesn't cause that problem. Is there any solution except from placing "if IsRWheld"? Thanks

The problem is something like that:

       class a
        {
    methodA()
    {
        RWLSlimObject.TryEnterWriteLock(-1);
        LockedList.Add(someItem)
        methodX();
        RWLSlimObject.ExitWriteLock();
    }

        methodX()
        {
        RWLSlimObject.TryEnterReadLock(-1);
        //some stuff with LockedList      //if called from method A, it will throw an exc.    
RWLSlimObject.ExitReadLock();
        }

    }
+2  A: 

The call to MethodX from MethodA qualifies it as recursive use of the lock.
See the remarks on the MSDN page for ReaderWriterLockSlim :

By default, new instances of ReaderWriterLockSlim are created with the LockRecursionPolicy.NoRecursion flag and do not allow recursion. This default policy is recommended for all new development, because recursion introduces unnecessary complications and makes your code more prone to deadlocks. To simplify migration from existing projects that use Monitor or ReaderWriterLock, you can use the LockRecursionPolicy.SupportsRecursion flag to create instances of ReaderWriterLockSlim that allow recursion.

Henk Holterman