ReaderWriterLock.ReleaseReaderLock 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
減量鎖定計數。
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 namespace System;
using namespace System::Threading;
public ref class Test
{
public:
// Declaring the ReaderWriterLock at the class level
// makes it visible to all threads.
static ReaderWriterLock^ rwl = gcnew ReaderWriterLock;
// For this example, the shared resource protected by the
// ReaderWriterLock is just an integer.
static int resource = 0;
// 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
// Shows how to request and release a reader lock, and
// how to 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( String::Format( "reads resource value {0}", resource ) );
Interlocked::Increment( reads );
}
finally
{
// Ensure that the lock is released.
rwl->ReleaseReaderLock();
}
}
catch ( ApplicationException^ )
{
// The reader lock request timed out.
Interlocked::Increment( readerTimeouts );
}
}
// 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 遞減鎖定計數。 當計數達到零時,就會釋放鎖定。
注意
如果執行緒具有寫入器鎖定,則呼叫 ReleaseReaderLock
的效果與呼叫 ReleaseWriterLock 相同。 如果執行緒沒有鎖定,則呼叫 ReleaseReaderLock
會擲回 ApplicationException 。