다음을 통해 공유


ReaderWriterLock.ReleaseReaderLock 메서드

정의

잠금 횟수를 줄입니다.

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

예외

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

예제

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

설명

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

참고

스레드에 기록기 잠금이 있는 경우 호출은 호출 ReleaseReaderLock ReleaseWriterLock과 동일한 효과가 있습니다. 스레드에 잠금이 없으면 호출 ReleaseReaderLock 이 throw됩니다 ApplicationException.

적용 대상

추가 정보