Partager via


ReaderWriterLock.IsWriterLockHeld Propriété

Définition

Obtient une valeur indiquant si le thread actif détient le verrou de writer.

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

Valeur de propriété

Boolean

true si le thread actif détient le verrou de writer ; sinon, false.

Exemples

L’exemple de code suivant montre que lorsqu’une tentative est effectuée pour acquérir un verrou de lecteur sur un thread doté d’un verrou d’enregistreur, n’accorde pas le verrou de lecteur, ReaderWriterLock mais incrémente plutôt le nombre de verrous sur le verrou d’enregistreur.

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

S’applique à

Voir aussi