ReaderWriterLock.IsWriterLockHeld 属性

定义

获取一个值,该值指示当前线程是否持有写线程锁。

public:
 property bool IsWriterLockHeld { bool get(); };
public bool IsWriterLockHeld { get; }
member this.IsWriterLockHeld : bool
Public ReadOnly Property IsWriterLockHeld As Boolean

属性值

Boolean

如果当前线程持有写线程锁,则为 true;否则为 false

示例

下面的代码示例演示了当尝试获取具有编写器锁的线程上的读取器锁时,不会授予读取器锁, ReaderWriterLock 而是递增写入器锁上的锁计数。

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

适用于

另请参阅