Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ReaderWriterLockSlim Constructor (LockRecursionPolicy)

 

Initializes a new instance of the ReaderWriterLockSlim class, specifying the lock recursion policy.

Namespace:   System.Threading
Assembly:  System.Core (in System.Core.dll)

public:
ReaderWriterLockSlim(
	LockRecursionPolicy recursionPolicy
)

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.

System_CAPS_noteNote

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.

No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.

The following code then uses the SynchronizedCache object to store a dictionary of vegetable names. It creates three tasks. The first writes the names of vegetables stored in an array to a SynchronizedCache instance. The second and third task display the names of the vegetables, the first in ascending order (from low index to high index), the second in descending order. The final task searches for the string "cucumber" and, when it finds it, calls the EnterUpgradeableReadLock method to substitute the string "green bean".

No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2017 Microsoft