ReaderWriterLock.UpgradeToWriterLock 메서드

정의

판독기 잠금을 작성기 잠금으로 업그레이드합니다.Upgrades a reader lock to the writer lock.

오버로드

UpgradeToWriterLock(Int32)

제한 시간에 Int32 값을 사용하여 판독기 잠금을 작성기 잠금으로 업그레이드합니다.Upgrades a reader lock to the writer lock, using an Int32 value for the time-out.

UpgradeToWriterLock(TimeSpan)

제한 시간에 TimeSpan 값을 사용하여 판독기 잠금을 작성기 잠금으로 업그레이드합니다.Upgrades a reader lock to the writer lock, using a TimeSpan value for the time-out.

UpgradeToWriterLock(Int32)

제한 시간에 Int32 값을 사용하여 판독기 잠금을 작성기 잠금으로 업그레이드합니다.Upgrades a reader lock to the writer lock, using an Int32 value for the time-out.

public:
 System::Threading::LockCookie UpgradeToWriterLock(int millisecondsTimeout);
public System.Threading.LockCookie UpgradeToWriterLock (int millisecondsTimeout);
member this.UpgradeToWriterLock : int -> System.Threading.LockCookie
Public Function UpgradeToWriterLock (millisecondsTimeout As Integer) As LockCookie

매개 변수

millisecondsTimeout
Int32

제한 시간(밀리초)입니다.The time-out in milliseconds.

반환

LockCookie

LockCookie 값입니다.A LockCookie value.

예외

잠금 요청이 부여되기 전에 millisecondsTimeout이 만료된 경우millisecondsTimeout expires before the lock request is granted.

예제

다음 코드 예제에서는 판독기 잠금을 요청 하 고 판독기 잠금을 작성기 잠금으로 업그레이드 한 다음 판독기 잠금으로 다시 다운 그레이드 하는 방법을 보여 줍니다.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

설명

스레드를 호출할 때 UpgradeToWriterLock 잠금 수에 관계 없이 판독기 잠금이 해제 되 고 스레드가 작성기 잠금에 대 한 큐의 끝으로 이동 합니다.When a thread calls UpgradeToWriterLock the reader lock is released, regardless of the lock count, and the thread goes to the end of the queue for the writer lock. 따라서 업그레이드를 요청한 스레드에 작성기 잠금이 부여 되기 전에 다른 스레드가 리소스에 쓸 수 있습니다.Thus, other threads might write to the resource before the thread that requested the upgrade is granted the writer lock.

중요

시간 제한 예외는 메서드를 호출한 스레드가 판독기 잠금을 다시 가져올 수 있을 때까지 throw 되지 않습니다 UpgradeToWriterLock .The time-out exception is not thrown until the thread that called the UpgradeToWriterLock method can reacquire the reader lock. 기록기 잠금을 기다리는 다른 스레드가 없으면 즉시 발생 합니다.If there are no other threads waiting for the writer lock, this happens immediately. 그러나 다른 스레드가 기록기 잠금에 대해 큐에 대기 중인 경우에는 UpgradeToWriterLock 모든 현재 판독기가 잠금을 해제 하 고 하나의 스레드가 작성기 잠금을 획득 하 고 해제 하기 전까지 메서드를 호출한 스레드가 판독기 잠금을 다시 가져올 수 없습니다.However, if another thread is queued for the writer lock, the thread that called the UpgradeToWriterLock method cannot reacquire the reader lock until all current readers have released their locks, and one thread has acquired and released the writer lock. 작성기 잠금을 요청한 다른 스레드가 메서드를 호출한 후에도이를 요청 하는 경우에도 마찬가지입니다 UpgradeToWriterLock .This is true even if the other thread that requested the writer lock requested it after the current thread called the UpgradeToWriterLock method.

잠금 상태를 복원 하려면 DowngradeFromWriterLock 에서 반환 된를 사용 하 여를 호출 합니다 LockCookie UpgradeToWriterLock .To restore the lock state, call DowngradeFromWriterLock using the LockCookie returned by UpgradeToWriterLock. 에는 사용 하지 LockCookie 마십시오 RestoreLock .Do not use this LockCookie with RestoreLock.

스레드에 판독기 잠금이 없으면를 사용 하지 마십시오 UpgradeToWriterLock .When a thread has no reader lock, do not use UpgradeToWriterLock. 대신 AcquireWriterLock를 사용하세요.Use AcquireWriterLock instead.

유효한 제한 시간 값은을 참조 하십시오 ReaderWriterLock .For valid time-out values, see ReaderWriterLock.

추가 정보

적용 대상

UpgradeToWriterLock(TimeSpan)

제한 시간에 TimeSpan 값을 사용하여 판독기 잠금을 작성기 잠금으로 업그레이드합니다.Upgrades a reader lock to the writer lock, using a TimeSpan value for the time-out.

public:
 System::Threading::LockCookie UpgradeToWriterLock(TimeSpan timeout);
public System.Threading.LockCookie UpgradeToWriterLock (TimeSpan timeout);
member this.UpgradeToWriterLock : TimeSpan -> System.Threading.LockCookie
Public Function UpgradeToWriterLock (timeout As TimeSpan) As LockCookie

매개 변수

timeout
TimeSpan

제한 시간을 지정하는 TimeSpan입니다.The TimeSpan specifying the time-out period.

반환

LockCookie

LockCookie 값입니다.A LockCookie value.

예외

잠금 요청이 부여되기 전에 timeout이 만료된 경우timeout expires before the lock request is granted.

timeout이 -1밀리초 이외의 음수 값을 지정하는 경우timeout specifies a negative value other than -1 milliseconds.

설명

스레드를 호출할 때 UpgradeToWriterLock 잠금 수에 관계 없이 판독기 잠금이 해제 되 고 스레드가 작성기 잠금에 대 한 큐의 끝으로 이동 합니다.When a thread calls UpgradeToWriterLock the reader lock is released, regardless of the lock count, and the thread goes to the end of the queue for the writer lock. 따라서 업그레이드를 요청한 스레드에 작성기 잠금이 부여 되기 전에 다른 스레드가 리소스에 쓸 수 있습니다.Thus, other threads might write to the resource before the thread that requested the upgrade is granted the writer lock.

중요

시간 제한 예외는 메서드를 호출한 스레드가 판독기 잠금을 다시 가져올 수 있을 때까지 throw 되지 않습니다 UpgradeToWriterLock .The time-out exception is not thrown until the thread that called the UpgradeToWriterLock method can reacquire the reader lock. 기록기 잠금을 기다리는 다른 스레드가 없으면 즉시 발생 합니다.If there are no other threads waiting for the writer lock, this happens immediately. 그러나 다른 스레드가 기록기 잠금에 대해 큐에 대기 중인 경우에는 UpgradeToWriterLock 모든 현재 판독기가 잠금을 해제 하 고 하나의 스레드가 작성기 잠금을 획득 하 고 해제 하기 전까지 메서드를 호출한 스레드가 판독기 잠금을 다시 가져올 수 없습니다.However, if another thread is queued for the writer lock, the thread that called the UpgradeToWriterLock method cannot reacquire the reader lock until all current readers have released their locks, and one thread has acquired and released the writer lock. 작성기 잠금을 요청한 다른 스레드가 메서드를 호출한 후에도이를 요청 하는 경우에도 마찬가지입니다 UpgradeToWriterLock .This is true even if the other thread that requested the writer lock requested it after the current thread called the UpgradeToWriterLock method.

잠금 상태를 복원 하려면 DowngradeFromWriterLock 에서 반환 된를 사용 하 여를 호출 합니다 LockCookie UpgradeToWriterLock .To restore the lock state, call DowngradeFromWriterLock using the LockCookie returned by UpgradeToWriterLock. 에는 사용 하지 LockCookie 마십시오 RestoreLock .Do not use this LockCookie with RestoreLock.

스레드에 판독기 잠금이 없으면를 사용 하지 마십시오 UpgradeToWriterLock .When a thread has no reader lock, do not use UpgradeToWriterLock. 대신 AcquireWriterLock를 사용하세요.Use AcquireWriterLock instead.

유효한 제한 시간 값은을 참조 하십시오 ReaderWriterLock .For valid time-out values, see ReaderWriterLock.

추가 정보

적용 대상