ReaderWriterLockSlim.ExitUpgradeableReadLock 方法

定義

減少可升級模式遞迴的計數,如果得出的計數為 0 (零),則結束可升級模式。

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

例外狀況

目前的執行緒尚未進入可升級模式的鎖定狀態。

範例

下列範例示範如何使用 finally 區塊來執行 ExitUpgradeableReadLock 方法,確保呼叫端結束可升級模式。

範例中顯示的 方法會擷取與索引鍵相關聯的值,並將其與新的值進行比較。 如果值未變更,方法會傳回狀態,指出沒有變更。 如果找不到索引鍵的值,則會插入索引鍵/值組。 如果值已變更,則會更新此值。 可升級模式可讓線程視需要升級讀取鎖定,而不會有死結的風險。

此範例會使用無參數建構函式來建立鎖定,因此不允許遞歸。 當鎖定不允許遞歸時,程式設計 ReaderWriterLockSlim 會比較簡單且較不容易發生錯誤。

此程式代碼是提供給 類別之較大範例的 ReaderWriterLockSlim 一部分。

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

備註

這個方法不區分遞歸順序。 例如,如果線程在可升級模式中進入鎖定,然後在寫入模式中進入鎖定,則線程結束這兩種模式的順序並不重要。 如果鎖定允許遞歸,線程可以在寫入模式中進入鎖定,然後在可升級模式中以遞歸方式輸入它;線程結束可升級模式和寫入模式的順序並不重要。

結束鎖定可能會發出其他等候線程的訊號。

適用於