ReaderWriterLock.DowngradeFromWriterLock(LockCookie) 메서드

정의

UpgradeToWriterLock(Int32)을 호출하기 전에 스레드의 잠금 상태를 이전 상태로 복원합니다.Restores the lock status of the thread to what it was before UpgradeToWriterLock(Int32) was called.

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

LockCookie에 의해 반환된 UpgradeToWriterLock(Int32)입니다.A LockCookie returned by UpgradeToWriterLock(Int32).

예외

스레드에 작성기 잠금이 없는 경우The thread does not have the writer lock.

lockCookie의 주소는 null 포인터입니다.The address of lockCookie is a null pointer.

예제

다음 코드 예제에서는 판독기 잠금을 요청 하 고 판독기 잠금을 작성기 잠금으로 업그레이드 한 다음 판독기 잠금으로 다시 다운 그레이드 하는 방법을 보여 줍니다.The following code example shows how to request a reader lock, upgrade the reader lock to a writer lock, and downgrade to a reader lock again.

이 코드는 클래스에 대해 제공 된 더 큰 예제의 일부입니다 ReaderWriterLock .This code is part of a larger example provided for the ReaderWriterLock class.

// The complete code is located in the ReaderWriterLock
// class topic.
using namespace System;
using namespace System::Threading;
public ref class Test
{
public:

   // Declaring the ReaderWriterLock at the class level
   // makes it visible to all threads.
   static ReaderWriterLock^ rwl = gcnew ReaderWriterLock;

   // For this example, the shared resource protected by the
   // ReaderWriterLock is just an integer.
   static int resource = 0;

// 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
// Shows how to request a reader lock, upgrade the
// reader lock to the writer lock, and downgrade to a
// reader lock again.
static void UpgradeDowngrade( Random^ rnd, int timeOut )
{
   try
   {
      rwl->AcquireReaderLock( timeOut );
      try
      {

         // It is safe for this thread to read from
         // the shared resource.
         Display( String::Format( "reads resource value {0}", resource ) );
         Interlocked::Increment( reads );

         // If it is necessary to write to the resource,
         // you must either release the reader lock and
         // then request the writer lock, or upgrade the
         // reader lock. Note that 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 is safe for this thread to read or write
               // from the shared resource.
               resource = rnd->Next( 500 );
               Display( String::Format( "writes resource value {0}", resource ) );
               Interlocked::Increment( writes );
            }
            finally
            {

               // Ensure that the lock is released.
               rwl->DowngradeFromWriterLock( lc );
            }

         }
         catch ( ApplicationException^ )
         {

            // The upgrade request timed out.
            Interlocked::Increment( writerTimeouts );
         }


         // When the lock has been downgraded, it is
         // still safe to read from the resource.
         Display( String::Format( "reads resource value {0}", resource ) );
         Interlocked::Increment( reads );
      }
      finally
      {

         // Ensure that the lock is released.
         rwl->ReleaseReaderLock();
      }

   }
   catch ( ApplicationException^ )
   {

      // The reader lock request timed out.
      Interlocked::Increment( readerTimeouts );
   }

}


// 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 releases the writer lock, regardless of the recursive lock count, and restores the reader lock that was held by the thread before upgrading to the writer lock. 판독기 잠금의 잠금 수가 복원 됩니다.The lock count on the reader lock is restored.

참고

DowngradeFromWriterLockLockCookie 호출 하 여 가져온를 수락 UpgradeToWriterLock 합니다.DowngradeFromWriterLock accepts a LockCookie obtained by calling UpgradeToWriterLock. 에서 반환 하는를 사용 하지 않습니다 LockCookie ReleaseLock .Do not use a LockCookie returned by ReleaseLock.

기록기 잠금이 해제 될 때 모든 판독기 잠금 요청이 허가 되기 때문에 다른 스레드가 기록기 잠금을 기다리는 경우에도 스레드는 기록기 잠금에서 다운 그레이드 시 차단 되지 않습니다.A thread does not block when downgrading from the writer lock, even if other threads are waiting for the writer lock, because all reader-lock requests are granted when the writer lock is released.

적용 대상

추가 정보