ReaderWriterLock.ReleaseWriterLock Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Décrémente le nombre de verrous sur le verrou de writer.
public:
void ReleaseWriterLock();
public void ReleaseWriterLock ();
member this.ReleaseWriterLock : unit -> unit
Public Sub ReleaseWriterLock ()
Exceptions
Le thread n'a pas de verrou de writer.
Exemples
L’exemple de code suivant montre comment acquérir et libérer un verrou d’enregistreur et comment gérer l’exception levée lorsqu’une demande expire.
Ce code fait partie d’un exemple plus grand fourni pour la ReaderWriterLock classe.
// The complete code is located in the ReaderWriterLock
// class topic.
using namespace System;
using namespace System::Threading;
public ref class Test
{
public:
// Declaring the ReaderWriterLock at the class level
// makes it visible to all threads.
static ReaderWriterLock^ rwl = gcnew ReaderWriterLock;
// For this example, the shared resource protected by the
// ReaderWriterLock is just an integer.
static int resource = 0;
// The complete code is located in the ReaderWriterLock class topic.
using System;
using System.Threading;
public class Example
{
static ReaderWriterLock rwl = new ReaderWriterLock();
// Define the shared resource protected by the ReaderWriterLock.
static int resource = 0;
' The complete code is located in the ReaderWriterLock class topic.
Imports System.Threading
Public Module Example
Private rwl As New ReaderWriterLock()
' Define the shared resource protected by the ReaderWriterLock.
Private resource As Integer = 0
// Shows how to request and release the writer lock, and
// how to handle time-outs.
static void WriteToResource( Random^ rnd, int timeOut )
{
try
{
rwl->AcquireWriterLock( timeOut );
try
{
// It is safe for this thread to read or write
// from the shared resource.
resource = rnd->Next( 500 );
Display( String::Format( "writes resource value {0}", resource ) );
Interlocked::Increment( writes );
}
finally
{
// Ensure that the lock is released.
rwl->ReleaseWriterLock();
}
}
catch ( ApplicationException^ )
{
// The writer lock request timed out.
Interlocked::Increment( writerTimeouts );
}
}
// Request and release the writer lock, and handle time-outs.
static void WriteToResource(Random rnd, int timeOut)
{
try {
rwl.AcquireWriterLock(timeOut);
try {
// It's safe for this thread to access from the shared resource.
resource = rnd.Next(500);
Display("writes resource value " + resource);
Interlocked.Increment(ref writes);
}
finally {
// Ensure that the lock is released.
rwl.ReleaseWriterLock();
}
}
catch (ApplicationException) {
// The writer lock request timed out.
Interlocked.Increment(ref writerTimeouts);
}
}
' Request and release the writer lock, and handle time-outs.
Sub WriteToResource(rnd As Random, timeOut As Integer)
Try
rwl.AcquireWriterLock(timeOut)
Try
' It's safe for this thread to read or write from the shared resource.
resource = rnd.Next(500)
Display("writes resource value " & resource)
Interlocked.Increment(writes)
Finally
' Ensure that the lock is released.
rwl.ReleaseWriterLock()
End Try
Catch ex As ApplicationException
' The writer lock request timed out.
Interlocked.Increment(writerTimeouts)
End Try
End Sub
};
}
End Module
Remarques
ReleaseWriterLock décrémente le nombre de verrous de l’enregistreur. Lorsque le nombre atteint zéro, le verrou de l’enregistreur est libéré.
Notes
Si un thread a un verrou de lecteur ou aucun verrou, l’appel ReleaseWriterLock
lève un ApplicationException.