संपादित करें

इसके माध्यम से साझा किया गया


ReaderWriterLockSlim.ExitUpgradeableReadLock Method

Definition

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 ()

Exceptions

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

Examples

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. If 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. Programming the ReaderWriterLockSlim is simpler and less prone to error when the lock does not allow recursion.

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

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 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.

Applies to