ReaderWriterLockSlim Constructor (LockRecursionPolicy)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Initializes a new instance of the ReaderWriterLockSlim class, specifying the lock recursion policy.
Assembly: System.Core (in System.Core.dll)
Parameters
- recursionPolicy
- Type: System.Threading.LockRecursionPolicy
One of the enumeration values that specifies the lock recursion policy.
Recursion policy determines the restrictions on threads that enter the lock more than once. For example, if a lock was created with LockRecursionPolicy.NoRecursion and a thread has entered the lock in read mode, LockRecursionException is thrown if the thread tries to reenter the lock in read mode. Similarly, if a thread has entered the lock in write mode, LockRecursionException is thrown if the thread tries to reenter the lock in any mode.
Note: |
|---|
A thread in upgradeable mode can upgrade to write mode or downgrade to read mode regardless of the lock recursion policy setting. |
Regardless of recursion policy, a thread that initially entered read mode is not allowed to upgrade to upgradeable mode or write mode, because that pattern creates a strong probability of deadlocks.
For more information about recursion policy and its effects, see the LockRecursionPolicy enumeration and the ReaderWriterLockSlim class.
The following example shows two exception scenarios, one that depends on the LockRecursionPolicy setting and one that does not.
In the first scenario, the thread enters read mode and then tries to enter read mode recursively. If the ReaderWriterLockSlim is created by using the default constructor, which sets recursion policy to LockRecursionPolicy.NoRecursion, an exception is thrown. If LockRecursionPolicy.SupportsRecursion is used to create the ReaderWriterLockSlim, no exception is thrown.
In the second scenario, the thread enters read mode and then tries to enter write mode. LockRecursionException is thrown regardless of the lock recursion policy.
Note: