ReaderWriterLock.ReleaseReaderLock Метод
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Уменьшает на единицу счетчик блокировок.
public:
void ReleaseReaderLock();
public void ReleaseReaderLock();
member this.ReleaseReaderLock : unit -> unit
Public Sub ReleaseReaderLock ()
Исключения
Поток не имеет блокировок чтения или записи.
Примеры
В следующем примере кода показано, как получить и освободить блокировку чтения и как обрабатывать исключение, возникающее при истечении времени ожидания запроса.
Этот код является частью более крупного примера, предоставленного ReaderWriterLock для класса.
// 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
Комментарии
ReleaseReaderLock уменьшает число блокировок. Когда число достигает нуля, блокировка освобождается.
Note
Если поток имеет блокировку записи, вызов имеет тот же эффект, что и вызовReleaseReaderLock.ReleaseWriterLock Если поток не имеет блокировок, вызов ReleaseReaderLock вызывает исключение ApplicationException.