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