Michael Damatov
Michael Damatov

Reputation: 15623

ReaderWriterLockSlim: acquiring a read lock after an upgradeable lock doesn't throw LockRecursionException

Concerning the ReaderWriterLockSlim:

Acquiring two locks subsequently within the same thread should actually throw a LockRecursionException (the recursion policy is set to NoRecursion).

My observation results:

Is this behavior correct?

Upvotes: 4

Views: 919

Answers (1)

anton.burger
anton.burger

Reputation: 5706

From the docs:

A thread in upgradeable mode can downgrade to read mode by first calling the EnterReadLock method and then calling the ExitUpgradeableReadLock method. This downgrade pattern is allowed for all lock recursion policies, even NoRecursion.

My understanding is that for the writing situation, entering a write lock is the normal way to move from upgradeable to write mode anyway, so has to be supported even under a policy of NoRecursion (there would seem to be little point to a non-upgradeable upgradeable lock :)

Upvotes: 4

Related Questions