ReaderWriterLockSlim.ExitUpgradeableReadLock 方法

定義

減少可升級模式遞迴的計數,如果得出的計數為 0 (零),則結束可升級模式。Reduces the recursion count for upgradeable mode, and exits upgradeable mode if the resulting count is 0 (zero).

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

例外狀況

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

範例

下列範例顯示如何使用finally區塊來ExitUpgradeableReadLock執行方法, 以確保呼叫端會結束可升級模式。The following example shows how to use a finally block to execute the ExitUpgradeableReadLock method, ensuring that the caller exits upgradeable mode.

範例中顯示的方法會抓取與索引鍵相關聯的值, 並將它與新值進行比較。The method shown in the example 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 the read lock as needed, without risk of deadlocks.

此範例會使用無參數的函式來建立鎖定, 因此不允許遞迴。The example uses the parameterless constructor to create the lock, so recursion is not allowed. 當鎖定不允許遞迴時,程式設計會比較簡單且較不容易發生錯誤。ReaderWriterLockSlimProgramming the ReaderWriterLockSlim is simpler and less prone to error when the lock does not allow recursion.

此程式碼是針對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 AddOrUpdateStatus AddOrUpdate(int key, string value)
{
    cacheLock.EnterUpgradeableReadLock();
    try
    {
        string result = null;
        if (innerCache.TryGetValue(key, out result))
        {
            if (result == value)
            {
                return AddOrUpdateStatus.Unchanged;
            }
            else
            {
                cacheLock.EnterWriteLock();
                try
                {
                    innerCache[key] = value;
                }
                finally
                {
                    cacheLock.ExitWriteLock();
                }
                return AddOrUpdateStatus.Updated;
            }
        }
        else
        {
            cacheLock.EnterWriteLock();
            try
            {
                innerCache.Add(key, value);
            }
            finally
            {
                cacheLock.ExitWriteLock();
            }
            return AddOrUpdateStatus.Added;
        }
    }
    finally
    {
        cacheLock.ExitUpgradeableReadLock();
    }
}
Public Function AddOrUpdate(ByVal key As Integer, _
                            ByVal value As String) As AddOrUpdateStatus
    cacheLock.EnterUpgradeableReadLock()
    Try
        Dim result As String = Nothing
        If innerCache.TryGetValue(key, result) Then
            If result = value Then
                Return AddOrUpdateStatus.Unchanged
            Else
                cacheLock.EnterWriteLock()
                Try
                    innerCache.Item(key) = value
                Finally
                    cacheLock.ExitWriteLock()
                End Try
                Return AddOrUpdateStatus.Updated
            End If
        Else
            cacheLock.EnterWriteLock()
            Try
                innerCache.Add(key, value)
            Finally
                cacheLock.ExitWriteLock()
            End Try
            Return AddOrUpdateStatus.Added
        End If
    Finally
        cacheLock.ExitUpgradeableReadLock()
    End Try
End Function
public enum AddOrUpdateStatus
{
    Added,
    Updated,
    Unchanged
};
Public Enum AddOrUpdateStatus
    Added
    Updated
    Unchanged
End Enum

備註

這個方法不是遞迴順序的敏感性。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 write 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 upgradeable mode; the order in which the thread exits upgradeable mode and write mode does not matter.

結束鎖定可能會通知其他等候中的執行緒。Exiting the lock might signal other waiting threads.

適用於