ReaderWriterLock.ReleaseReaderLock Método

Definición

Reduce el recuento de bloqueos.

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

Excepciones

El subproceso no tiene ningún bloqueo de lector o escritor.

Ejemplos

En el ejemplo de código siguiente se muestra cómo adquirir y liberar un bloqueo de lector y cómo controlar la excepción que se produce cuando se agota el tiempo de espera de una solicitud.

Este código forma parte de un ejemplo más grande proporcionado para la ReaderWriterLock clase .

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

Comentarios

ReleaseReaderLock disminuye el recuento de bloqueos. Cuando el recuento alcanza cero, se libera el bloqueo.

Note

Si un subproceso tiene el bloqueo de escritura, llamar a ReleaseReaderLock tiene el mismo efecto que llamar a ReleaseWriterLock. Si un subproceso no tiene bloqueos, la llamada ReleaseReaderLock produce una ApplicationExceptionexcepción .

Se aplica a

Consulte también