ReaderWriterLock.ReleaseReaderLock Metoda

Definicja

Dekrementuje liczbę blokad.

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

Wyjątki

Wątek nie ma żadnych blokad czytnika ani zapisywania.

Przykłady

Poniższy przykład kodu pokazuje, jak uzyskać i zwolnić blokadę czytnika oraz jak obsługiwać wyjątek zgłaszany po upłynął limit czasu żądania.

Ten kod jest częścią większego przykładu udostępnionego ReaderWriterLock dla klasy .

// 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
// 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

Uwagi

ReleaseReaderLock dekrementuje liczbę blokad. Gdy liczba osiągnie zero, blokada zostanie zwolniona.

Uwaga / Notatka

Jeśli wątek ma blokadę modułu zapisywania, wywołanie ReleaseReaderLock ma taki sam efekt jak wywołanie metody ReleaseWriterLock. Jeśli wątek nie ma blokad, wywołanie ReleaseReaderLock zgłasza błąd ApplicationException.

Dotyczy

Zobacz też