ReaderWriterLock.IsReaderLockHeld Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets a value indicating whether the current thread holds a reader lock.
public:
property bool IsReaderLockHeld { bool get(); };
public bool IsReaderLockHeld { get; }
member this.IsReaderLockHeld : bool
Public ReadOnly Property IsReaderLockHeld As Boolean
Property Value
true
if the current thread holds a reader lock; otherwise, false
.
Examples
The following code example demonstrates how to use IsReaderLockHeld
to avoid deadlocks.
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
Applies to
See also
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET