ReaderWriterLockSlim.ExitReadLock 方法

定義

減少讀取模式遞迴的計數,如果得出的計數為 0 (零),則結束讀取模式。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 ()

例外狀況

目前的執行緒尚未進入讀取模式的鎖定狀態。The current thread has not entered the lock in read mode.

範例

下列範例顯示如何使用finally區塊來ExitReadLock執行方法, 以確保呼叫端結束讀取模式。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. 如果找不到索引鍵, 則會允許內部Dictionary<TKey,TValue>擲回的例外狀況終止方法。If the key is not found, the exception thrown by the inner Dictionary<TKey,TValue> is allowed to terminate the method. EnterReadLock方法是用來進入讀取模式。The EnterReadLock method is used to enter read mode.

此程式碼是針對ReaderWriterLockSlim類別提供的較大範例的一部分。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

備註

這個方法不是遞迴順序的敏感性。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.

適用於