ReaderWriterLock.IsReaderLockHeld 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出目前的執行緒是否掌握讀取器的鎖定。
public:
property bool IsReaderLockHeld { bool get(); };
public bool IsReaderLockHeld { get; }
member this.IsReaderLockHeld : bool
Public ReadOnly Property IsReaderLockHeld As Boolean
屬性值
如果目前的執行緒掌握讀取器的鎖定,則為 true
,否則為 false
。
範例
下列程式碼範例示範如何使用 IsReaderLockHeld
來避免死結。
using namespace System;
using namespace System::Threading;
int main()
{
ReaderWriterLock^ rwLock = gcnew ReaderWriterLock;
rwLock->AcquireWriterLock( Timeout::Infinite );
rwLock->AcquireReaderLock( Timeout::Infinite );
if ( rwLock->IsReaderLockHeld )
{
Console::WriteLine( "Reader lock held." );
rwLock->ReleaseReaderLock();
}
else
if ( rwLock->IsWriterLockHeld )
{
Console::WriteLine( "Writer lock held." );
rwLock->ReleaseWriterLock();
}
else
{
Console::WriteLine( "No locks held." );
}
if ( rwLock->IsReaderLockHeld )
{
Console::WriteLine( "Reader lock held." );
rwLock->ReleaseReaderLock();
}
else
if ( rwLock->IsWriterLockHeld )
{
Console::WriteLine( "Writer lock held." );
rwLock->ReleaseWriterLock();
}
else
{
Console::WriteLine( "No locks held." );
}
}
using System;
using System.Threading;
class LockHeld
{
static void Main()
{
ReaderWriterLock rwLock = new ReaderWriterLock();
rwLock.AcquireWriterLock(Timeout.Infinite);
rwLock.AcquireReaderLock(Timeout.Infinite);
if(rwLock.IsReaderLockHeld)
{
Console.WriteLine("Reader lock held.");
rwLock.ReleaseReaderLock();
}
else if(rwLock.IsWriterLockHeld)
{
Console.WriteLine("Writer lock held.");
rwLock.ReleaseWriterLock();
}
else
{
Console.WriteLine("No locks held.");
}
if(rwLock.IsReaderLockHeld)
{
Console.WriteLine("Reader lock held.");
rwLock.ReleaseReaderLock();
}
else if(rwLock.IsWriterLockHeld)
{
Console.WriteLine("Writer lock held.");
rwLock.ReleaseWriterLock();
}
else
{
Console.WriteLine("No locks held.");
}
}
}
Imports System.Threading
Public Class LockHeld
<MTAThread> _
Shared Sub Main()
Dim rwLock As New ReaderWriterLock()
rwLock.AcquireWriterLock(Timeout.Infinite)
rwLock.AcquireReaderLock(Timeout.Infinite)
If rwLock.IsReaderLockHeld Then
Console.WriteLine("Reader lock held.")
rwLock.ReleaseReaderLock()
Else If rwLock.IsWriterLockHeld Then
Console.WriteLine("Writer lock held.")
rwLock.ReleaseWriterLock()
Else
Console.WriteLine("No locks held.")
End If
If rwLock.IsReaderLockHeld Then
Console.WriteLine("Reader lock held.")
rwLock.ReleaseReaderLock()
Else If rwLock.IsWriterLockHeld Then
Console.WriteLine("Writer lock held.")
rwLock.ReleaseWriterLock()
Else
Console.WriteLine("No locks held.")
End If
End Sub
End Class