다음을 통해 공유


ReaderWriterLock.ReleaseWriterLock 메서드

정의

작성기 잠금의 잠금 횟수를 줄입니다.

public:
 void ReleaseWriterLock();
public void ReleaseWriterLock ();
member this.ReleaseWriterLock : unit -> unit
Public Sub ReleaseWriterLock ()

예외

스레드에 작성기 잠금이 없는 경우

예제

다음 코드 예제에서는 기록기 잠금을 획득 및 해제하는 방법과 요청 시간이 초과될 때 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

설명

ReleaseWriterLock 는 기록기 잠금 수를 감소합니다. 개수가 0에 도달하면 기록기 잠금이 해제됩니다.

참고

스레드에 판독기 잠금이 있거나 잠금이 없으면 호출 ReleaseWriterLockApplicationExceptionthrow됩니다.

적용 대상

추가 정보