ReaderWriterLock.AcquireReaderLock 메서드

정의

판독기 잠금을 가져옵니다.

오버로드

AcquireReaderLock(Int32)

제한 시간에 Int32 값을 사용하여 판독기 잠금을 가져옵니다.

AcquireReaderLock(TimeSpan)

제한 시간에 TimeSpan 값을 사용하여 판독기 잠금을 가져옵니다.

AcquireReaderLock(Int32)

제한 시간에 Int32 값을 사용하여 판독기 잠금을 가져옵니다.

public:
 void AcquireReaderLock(int millisecondsTimeout);
public void AcquireReaderLock (int millisecondsTimeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void AcquireReaderLock (int millisecondsTimeout);
member this.AcquireReaderLock : int -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.AcquireReaderLock : int -> unit
Public Sub AcquireReaderLock (millisecondsTimeout As Integer)

매개 변수

millisecondsTimeout
Int32

제한 시간(밀리초)입니다.

특성

예외

잠금 요청이 부여되기 전에 millisecondsTimeout이 만료된 경우

예제

다음 코드 예제에서는 판독기 잠금을 획득 및 해제하는 방법과 요청 시간이 초과될 때 throw된 예외를 처리하는 방법을 보여 줍니다.

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

// 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 and release a reader lock, and
// how to handle time-outs.
static void ReadFromResource( 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 );
      }
      finally
      {

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

   }
   catch ( ApplicationException^ )
   {

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

}
// Request and release a reader lock, and handle time-outs.
static void ReadFromResource(int timeOut)
{
   try {
      rwl.AcquireReaderLock(timeOut);
      try {
         // It is safe for this thread to read from the shared 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);
   }
}
' Request and release a reader lock, and handle time-outs.
Sub ReadFromResource(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)
      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

설명

AcquireReaderLock 는 다른 스레드에 기록기 잠금이 있거나 하나 이상의 스레드가 기록기 잠금을 기다리는 경우 입니다.

참고

현재 스레드에 이미 기록기 잠금이 있는 경우 판독기 잠금이 획득되지 않습니다. 대신 기록기 잠금의 잠금 수가 증가합니다. 이렇게 하면 스레드가 자체 작성기 잠금에서 차단되지 않습니다. 결과는 호출 AcquireWriterLock과 정확히 동일하며 작성기 잠금을 해제할 ReleaseWriterLock 때 추가 호출이 필요합니다.

AcquireReaderLock 는 재귀 판독기 잠금 요청을 지원합니다. 즉, 스레드는 AcquireReaderLock을 여러 번 호출하여 매번 잠금 수를 증분할 수 있습니다. 호출할 때마다 한 번 호출 ReleaseReaderLock AcquireReaderLock해야 합니다. 또는 잠금 수를 즉시 0으로 줄이기 위해 호출 ReleaseLock 할 수 있습니다.

재귀 잠금 요청은 요청 스레드를 판독기 큐에 배치하지 않고 항상 즉시 부여됩니다. 장기간 기록기 잠금 요청을 차단하지 않도록 하려면 재귀 잠금을 주의해서 사용합니다.

유효한 제한 시간 값은 을 참조하세요 ReaderWriterLock.

추가 정보

적용 대상

AcquireReaderLock(TimeSpan)

제한 시간에 TimeSpan 값을 사용하여 판독기 잠금을 가져옵니다.

public:
 void AcquireReaderLock(TimeSpan timeout);
public void AcquireReaderLock (TimeSpan timeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void AcquireReaderLock (TimeSpan timeout);
member this.AcquireReaderLock : TimeSpan -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.AcquireReaderLock : TimeSpan -> unit
Public Sub AcquireReaderLock (timeout As TimeSpan)

매개 변수

timeout
TimeSpan

제한 시간을 지정하는 TimeSpan입니다.

특성

예외

잠금 요청이 부여되기 전에 timeout이 만료된 경우

timeout이 -1밀리초 이외의 음수 값을 지정하는 경우

설명

AcquireReaderLock 는 다른 스레드에 기록기 잠금이 있거나 하나 이상의 스레드가 기록기 잠금을 기다리는 경우 입니다.

참고

현재 스레드에 이미 기록기 잠금이 있는 경우 판독기 잠금이 획득되지 않습니다. 대신 기록기 잠금의 잠금 수가 증가합니다. 이렇게 하면 스레드가 자체 작성기 잠금에서 차단되지 않습니다. 결과는 호출 AcquireWriterLock과 정확히 동일하며 작성기 잠금을 해제할 ReleaseWriterLock 때 추가 호출이 필요합니다.

AcquireReaderLock 는 재귀 판독기 잠금 요청을 지원합니다. 즉, 스레드는 AcquireReaderLock을 여러 번 호출하여 매번 잠금 수를 증분할 수 있습니다. 호출할 때마다 한 번 호출 ReleaseReaderLock AcquireReaderLock해야 합니다. 또는 잠금 수를 즉시 0으로 줄이기 위해 호출 ReleaseLock 할 수 있습니다.

재귀 잠금 요청은 요청 스레드를 판독기 큐에 배치하지 않고 항상 즉시 부여됩니다. 장기간 기록기 잠금 요청을 차단하지 않도록 하려면 재귀 잠금을 주의해서 사용합니다.

유효한 제한 시간 값은 을 참조하세요 ReaderWriterLock.

추가 정보

적용 대상