ReaderWriterLock.DowngradeFromWriterLock(LockCookie) 메서드

정의

스레드의 잠금 상태를 호출된 이전 UpgradeToWriterLock(Int32) 상태로 복원합니다.

public:
 void DowngradeFromWriterLock(System::Threading::LockCookie % lockCookie);
public void DowngradeFromWriterLock(ref System.Threading.LockCookie lockCookie);
member this.DowngradeFromWriterLock : LockCookie -> unit
Public Sub DowngradeFromWriterLock (ByRef lockCookie As LockCookie)

매개 변수

lockCookie
LockCookie

에 의해 반환된 LockCookieA UpgradeToWriterLock(Int32) 입니다.

예외

스레드에 기록기 잠금이 없습니다.

주소 lockCookie 는 null 포인터입니다.

예제

다음 코드 예제에서는 판독기 잠금을 요청하고, 판독기 잠금을 기록기 잠금으로 업그레이드하고, 판독기 잠금으로 다시 다운그레이드하는 방법을 보여 있습니다.

이 코드는 클래스에 제공된 더 큰 예제의 ReaderWriterLock 일부입니다.

// The complete code is located in the ReaderWriterLock class topic.
using System;
using System.Threading;

public class Example
{
   static ReaderWriterLock rwl = new ReaderWriterLock();
   // Define the shared resource protected by the ReaderWriterLock.
   static int resource = 0;
' The complete code is located in the ReaderWriterLock class topic.
Imports System.Threading

Public Module Example
   Private rwl As New ReaderWriterLock()
   ' Define the shared resource protected by the ReaderWriterLock.
   Private resource As Integer = 0
// Requests a reader lock, upgrades the reader lock to the writer
// lock, and downgrades it to a reader lock again.
static void UpgradeDowngrade(Random rnd, int timeOut)
{
   try {
      rwl.AcquireReaderLock(timeOut);
      try {
         // It's safe for this thread to read from the shared resource.
         Display("reads resource value " + resource);
         Interlocked.Increment(ref reads);

         // To write to the resource, either release the reader lock and
         // request the writer lock, or upgrade the reader lock. Upgrading
         // the reader lock puts the thread in the write queue, behind any
         // other threads that might be waiting for the writer lock.
         try {
            LockCookie lc = rwl.UpgradeToWriterLock(timeOut);
            try {
               // It's safe for this thread to read or write from the shared resource.
               resource = rnd.Next(500);
               Display("writes resource value " + resource);
               Interlocked.Increment(ref writes);
            }
            finally {
               // Ensure that the lock is released.
               rwl.DowngradeFromWriterLock(ref lc);
            }
         }
         catch (ApplicationException) {
            // The upgrade request timed out.
            Interlocked.Increment(ref writerTimeouts);
         }

         // If the lock was downgraded, it's still safe to read from the resource.
         Display("reads resource value " + resource);
         Interlocked.Increment(ref reads);
      }
      finally {
         // Ensure that the lock is released.
         rwl.ReleaseReaderLock();
      }
   }
   catch (ApplicationException) {
      // The reader lock request timed out.
      Interlocked.Increment(ref readerTimeouts);
   }
}
' Requests a reader lock, upgrades the reader lock to the writer
' lock, and downgrades it to a reader lock again.
Sub UpgradeDowngrade(rnd As Random, timeOut As Integer)
   Try
      rwl.AcquireReaderLock(timeOut)
      Try
         ' It's safe for this thread to read from the shared resource.
         Display("reads resource value " & resource)
         Interlocked.Increment(reads)
         
         ' To write to the resource, either release the reader lock and
         ' request the writer lock, or upgrade the reader lock. Upgrading
         ' the reader lock puts the thread in the write queue, behind any
         ' other threads that might be waiting for the writer lock.
         Try
            Dim lc As LockCookie = rwl.UpgradeToWriterLock(timeOut)
            Try
               ' It's safe for this thread to read or write from the shared resource.
               resource = rnd.Next(500)
               Display("writes resource value " & resource)
               Interlocked.Increment(writes)
            Finally
               ' Ensure that the lock is released.
               rwl.DowngradeFromWriterLock(lc)
            End Try
         Catch ex As ApplicationException
            ' The upgrade request timed out.
            Interlocked.Increment(writerTimeouts)
         End Try
         
         ' If the lock was downgraded, it's still safe to read from the resource.
         Display("reads resource value " & resource)
         Interlocked.Increment(reads)
      Finally
         ' Ensure that the lock is released.
         rwl.ReleaseReaderLock()
      End Try
   Catch ex As ApplicationException
      ' The reader lock request timed out.
      Interlocked.Increment(readerTimeouts)
   End Try
End Sub
}
End Module

설명

DowngradeFromWriterLock 는 재귀 잠금 수에 관계없이 기록기 잠금을 해제하고 기록기 잠금으로 업그레이드하기 전에 스레드에서 보유한 판독기 잠금을 복원합니다. 판독기 잠금의 잠금 수가 복원됩니다.

메모

DowngradeFromWriterLock 은 (을) LockCookie 호출 UpgradeToWriterLock하여 가져온 것을 허용합니다. 에서 반환LockCookie한 것을 ReleaseLock 사용하지 마세요.

기록기 잠금이 해제될 때 모든 판독기 잠금 요청이 부여되기 때문에 다른 스레드가 기록기 잠금을 기다리는 경우에도 기록기 잠금에서 다운그레이드할 때 스레드가 차단되지 않습니다.

적용 대상

추가 정보

  • 관리되는 스레딩