ReaderWriterLock.ReleaseWriterLock Метод
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Уменьшает количество блокировок на блокировке записи.
public:
void ReleaseWriterLock();
public void ReleaseWriterLock();
member this.ReleaseWriterLock : unit -> unit
Public Sub ReleaseWriterLock ()
Исключения
Поток не имеет блокировки записи.
Примеры
В следующем примере кода показано, как получить и освободить блокировку записи и как обрабатывать исключение, возникающее при истечении времени ожидания запроса.
Этот код является частью более крупного примера, предоставленного 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 the writer lock, and handle time-outs.
static void WriteToResource(Random rnd, int timeOut)
{
try {
rwl.AcquireWriterLock(timeOut);
try {
// It's safe for this thread to access from the shared resource.
resource = rnd.Next(500);
Display("writes resource value " + resource);
Interlocked.Increment(ref writes);
}
finally {
// Ensure that the lock is released.
rwl.ReleaseWriterLock();
}
}
catch (ApplicationException) {
// The writer lock request timed out.
Interlocked.Increment(ref writerTimeouts);
}
}
' Request and release the writer lock, and handle time-outs.
Sub WriteToResource(rnd As Random, timeOut As Integer)
Try
rwl.AcquireWriterLock(timeOut)
Try
' It's safe for this thread to read or write from the shared resource.
resource = rnd.Next(500)
Display("writes resource value " & resource)
Interlocked.Increment(writes)
Finally
' Ensure that the lock is released.
rwl.ReleaseWriterLock()
End Try
Catch ex As ApplicationException
' The writer lock request timed out.
Interlocked.Increment(writerTimeouts)
End Try
End Sub
}
End Module
Комментарии
ReleaseWriterLock уменьшает число блокировок записи. Когда число достигает нуля, блокировка записи освобождается.
Note
Если поток имеет блокировку чтения или нет блокировок, вызов ReleaseWriterLock вызывает исключение ApplicationException.