ReaderWriterLockSlim.EnterUpgradeableReadLock 方法

定義

嘗試進入可升級模式的鎖定狀態。Tries to enter the lock in upgradeable mode.

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

例外狀況

RecursionPolicy 屬性為 NoRecursion,且目前的執行緒已進入任何模式的鎖定狀態。The RecursionPolicy property is NoRecursion and the current thread has already entered the lock in any mode.

-或--or- 目前執行緒已進入讀取模式,因此嘗試進入可升級模式可能會造成死結。The current thread has entered read mode, so trying to enter upgradeable mode would create the possibility of a deadlock.

-或--or- 遞迴的次數會超出計數器的容量。The recursion number would exceed the capacity of the counter. 限制過大,使應用程式永遠不會發生這個情形。The limit is so large that applications should never encounter it.

ReaderWriterLockSlim 物件已經處置。The ReaderWriterLockSlim object has been disposed.

範例

下列範例顯示如何使用EnterUpgradeableReadLock方法, 進入可升級模式的鎖定。The following example shows how to use the EnterUpgradeableReadLock method to enter the lock in upgradeable mode. 區塊是用來ExitUpgradeableReadLock執行方法, 以確保呼叫端會結束可升級模式。 finallyA finally block is used 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 blocks until the calling thread enters the lock, and therefore might never return. TryEnterUpgradeableReadLock使用方法來封鎖指定的間隔, 然後在呼叫執行緒未在該間隔期間進入可升級模式時傳回。Use the TryEnterUpgradeableReadLock method to block for a specified interval, and then return if the calling thread has not entered upgradeable mode during that interval.

當執行緒通常會存取ReaderWriterLockSlim以讀取模式保護的資源時, 請使用可升級模式, 但如果符合特定條件, 可能需要進入寫入模式。Use upgradeable mode when a thread usually accesses the resource that is protected by the ReaderWriterLockSlim in read mode, but may need to enter write mode if certain conditions are met. 處於可升級模式的執行緒可以降級為讀取模式, 或升級為寫入模式。A thread in upgradeable mode can downgrade to read mode or upgrade to write mode.

在任何指定時間, 只有一個執行緒可以進入可升級模式。Only one thread can enter upgradeable mode at any given time. 如果執行緒處於可升級模式, 而且沒有任何執行緒等候進入寫入模式, 則任何數目的其他執行緒都可以進入讀取模式, 即使有線程等候進入可升級模式也一樣。If a thread is in upgradeable mode, and there are no threads waiting to enter write mode, any number of other threads can enter read mode, even if there are threads waiting to enter upgradeable mode.

如果有一或多個執行緒正在等候進入寫入模式, 則呼叫EnterUpgradeableReadLock方法的執行緒會封鎖, 直到這些執行緒有超時或進入寫入模式, 然後再從中結束為止。If one or more threads are waiting to enter write mode, a thread that calls the EnterUpgradeableReadLock method blocks until those threads have either timed out or entered write mode and then exited from it.

注意

如果鎖定允許遞迴, 已進入可升級模式鎖定的執行緒可以遞迴進入可升級模式, 即使其他執行緒正在等候進入寫入模式也一樣。If a lock allows recursion, a thread that has entered the lock in upgradeable mode can enter upgradeable mode recursively, even if other threads are waiting to enter write mode.

適用於