ReaderWriterLock.ReleaseReaderLock Metodo

Definizione

Consente di diminuire il conteggio dei blocchi.

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

Eccezioni

Il thread non dispone di blocchi lettore o writer.

Esempio

Nell'esempio di codice seguente viene illustrato come acquisire e rilasciare un blocco lettore e come gestire l'eccezione generata quando si verifica il timeout di una richiesta.

Questo codice fa parte di un esempio più ampio fornito per la ReaderWriterLock classe .

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

Commenti

ReleaseReaderLock decrementa il conteggio dei blocchi. Quando il conteggio raggiunge lo zero, il blocco viene rilasciato.

Note

Se un thread ha il blocco writer, la chiamata ReleaseReaderLock ha lo stesso effetto della chiamata ReleaseWriterLocka . Se un thread non ha blocchi, la chiamata ReleaseReaderLock genera un'eccezione ApplicationException.

Si applica a

Vedi anche