Редагувати

Поділитися через


ReaderWriterLockSlim.ExitReadLock Method

Definition

Reduces the recursion count for read mode, and exits read mode if the resulting count is 0 (zero).

public:
 void ExitReadLock();
public void ExitReadLock ();
member this.ExitReadLock : unit -> unit
Public Sub ExitReadLock ()

Exceptions

The current thread has not entered the lock in read mode.

Examples

The following example shows how to use a finally block to execute the ExitReadLock method, ensuring that the caller exits read mode. The method shown in the example retrieves the value associated with a key. If the key is not found, the exception thrown by the inner Dictionary<TKey,TValue> is allowed to terminate the method. The EnterReadLock method is used to enter read mode.

This code is part of a larger example provided for the ReaderWriterLockSlim class.

private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
private Dictionary<int, string> innerCache = new Dictionary<int, string>();
Private cacheLock As New ReaderWriterLockSlim()
Private innerCache As New Dictionary(Of Integer, String)
public string Read(int key)
{
    cacheLock.EnterReadLock();
    try
    {
        return innerCache[key];
    }
    finally
    {
        cacheLock.ExitReadLock();
    }
}
Public Function Read(ByVal key As Integer) As String
    cacheLock.EnterReadLock()
    Try
        Return innerCache(key)
    Finally
        cacheLock.ExitReadLock()
    End Try
End Function

Remarks

This method is not sensitive to recursion order. For example, if a thread enters a lock in upgradeable mode and then enters the lock in read mode, the order in which the thread exits the two modes does not matter. If a lock allows recursion, a thread can enter the lock in write mode and then enter it recursively in read mode; the order in which the thread exits read mode and write mode does not matter.

Exiting the lock might signal other waiting threads.

Applies to