ReaderWriterLock.AcquireWriterLock 메서드

정의

작성기 잠금을 가져옵니다.

오버로드

AcquireWriterLock(Int32)

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

AcquireWriterLock(TimeSpan)

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

AcquireWriterLock(Int32)

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

public:
 void AcquireWriterLock(int millisecondsTimeout);
public void AcquireWriterLock (int millisecondsTimeout);
member this.AcquireWriterLock : int -> unit
Public Sub AcquireWriterLock (millisecondsTimeout As Integer)

매개 변수

millisecondsTimeout
Int32

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

예외

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

예제

다음 코드 예제에서는 기록기 잠금을 획득 및 해제하는 방법과 요청 시간이 초과될 때 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 the writer lock, and
// how to handle time-outs.
static void WriteToResource( Random^ rnd, int timeOut )
{
   try
   {
      rwl->AcquireWriterLock( 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->ReleaseWriterLock();
      }

   }
   catch ( ApplicationException^ )
   {

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

}
// Request and release the writer lock, and handle time-outs.
static void WriteToResource(Random rnd, int timeOut)
{
   try {
      rwl.AcquireWriterLock(timeOut);
      try {
         // It's safe for this thread to access 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.ReleaseWriterLock();
      }
   }
   catch (ApplicationException) {
      // The writer lock request timed out.
      Interlocked.Increment(ref writerTimeouts);
   }
}
' Request and release the writer lock, and handle time-outs.
Sub WriteToResource(rnd As Random, timeOut As Integer)
   Try
      rwl.AcquireWriterLock(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.ReleaseWriterLock()
      End Try
   Catch ex As ApplicationException
      ' The writer lock request timed out.
      Interlocked.Increment(writerTimeouts)
   End Try
End Sub
};
}
End Module

설명

이 메서드는 다른 스레드에 판독기 잠금 또는 기록기 잠금이 있는 경우 차단합니다. 기록기 잠금이 여러 동시 판독기 잠금과 대체되는 방식에 대한 설명은 클래스를 ReaderWriterLock 참조하세요.

판독기 잠금이 이미 있는 스레드는 호출하기 전에 판독기 잠금을 해제하거나 호출AcquireWriterLockUpgradeToWriterLock하는 두 가지 방법 중 하나로 기록기 잠금을 획득할 수 있습니다.

주의

스레드가 여전히 판독기 잠금이 있는 동안 호출 AcquireWriterLock 하는 경우 자체 판독기 잠금에서 차단됩니다. 무한 제한 시간이 지정되면 스레드가 교착 상태가 됩니다. 이러한 교착 상태를 방지하려면 현재 스레드에 이미 판독기 잠금이 있는지 여부를 확인하는 데 사용합니다 IsReaderLockHeld .

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

재귀 잠금 요청은 요청 스레드를 기록기 큐에 배치하지 않고 항상 즉시 부여됩니다.

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

추가 정보

적용 대상

AcquireWriterLock(TimeSpan)

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

public:
 void AcquireWriterLock(TimeSpan timeout);
public void AcquireWriterLock (TimeSpan timeout);
member this.AcquireWriterLock : TimeSpan -> unit
Public Sub AcquireWriterLock (timeout As TimeSpan)

매개 변수

timeout
TimeSpan

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

예외

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

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

설명

이 메서드는 다른 스레드에 판독기 잠금 또는 기록기 잠금이 있는 경우 차단합니다. 기록기 잠금이 여러 동시 판독기 잠금과 대체되는 방식에 대한 설명은 클래스를 ReaderWriterLock 참조하세요.

판독기 잠금이 이미 있는 스레드는 호출하기 전에 판독기 잠금을 해제하거나 호출AcquireWriterLockUpgradeToWriterLock하는 두 가지 방법 중 하나로 기록기 잠금을 획득할 수 있습니다.

주의

스레드가 여전히 판독기 잠금이 있는 동안 호출 AcquireWriterLock 하는 경우 자체 판독기 잠금에서 차단됩니다. 무한 제한 시간이 지정되면 스레드가 교착 상태가 됩니다. 이러한 교착 상태를 방지하려면 현재 스레드에 이미 판독기 잠금이 있는지 여부를 확인하는 데 사용합니다 IsReaderLockHeld .

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

재귀 잠금 요청은 요청 스레드를 기록기 큐에 배치하지 않고 항상 즉시 부여됩니다.

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

추가 정보

적용 대상