ReaderWriterLockSlim Constructor
[ 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 with default property values.
Assembly: System.Core (in System.Core.dll)
A ReaderWriterLockSlim that is initialized with this constructor does not allow recursion. That is, the RecursionPolicy property returns LockRecursionPolicy.NoRecursion.
For more information about recursion policy and its effects, see the LockRecursionPolicy enumeration and the ReaderWriterLockSlim class.
The following example shows a simple synchronized cache that holds strings with integer keys. An instance of ReaderWriterLockSlim is used to synchronize access to the Dictionary(Of TKey, TValue) that serves as the inner cache. The parameterless constructor is used to create the lock.
The example includes simple methods to add to the cache, delete from the cache, and read from the cache. To demonstrate time-outs, the example includes a method that adds to the cache only if it can do so within a specified time-out.
To demonstrate upgradeable mode, the example includes a method that retrieves the value associated with a key and compares it with a new value. If the value is unchanged, the method returns a status indicating no change. It no value is found for the key, the key/value pair is inserted. If the value has changed, it is updated. Upgradeable mode allows the thread to upgrade from read access to write access as needed, without the risk of deadlocks.
The example includes a nested enumeration that specifies the return values for the method that demonstrates upgradeable mode.
The example uses the default constructor to create the lock, so recursion is not allowed. Programming the ReaderWriterLockSlim is simpler and less prone to error when the lock does not allow recursion.