ReaderWriterLock.ReleaseReaderLock Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Diminui a contagem de fechaduras.
public:
void ReleaseReaderLock();
public void ReleaseReaderLock();
member this.ReleaseReaderLock : unit -> unit
Public Sub ReleaseReaderLock ()
Exceções
O tópico não tem bloqueios para leitores ou escritores.
Exemplos
O exemplo de código seguinte mostra como adquirir e libertar um bloqueio de leitor, e como lidar com a exceção lançada quando um pedido expira.
Este código faz parte de um exemplo mais amplo fornecido para a ReaderWriterLock turma.
// 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
Observações
ReleaseReaderLock diminui a contagem de fechaduras. Quando a contagem chega a zero, a fechadura é libertada.
Note
Se uma thread tem o bloqueio de escritor, chamar ReleaseReaderLock tem o mesmo efeito que chamar ReleaseWriterLock. Se um thread não tiver bloqueios, chamar ReleaseReaderLock lança um ApplicationException.
Aplica-se a
Ver também
- de threading gerenciado
- ReaderWriterLock