ReaderWriterLock::ReleaseWriterLock Method ()

 

Decrements the lock count on the writer lock.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public:
void ReleaseWriterLock()

Exception Condition
ApplicationException

The thread does not have the writer lock.

ReleaseWriterLock decrements the writer lock count. When the count reaches zero, the writer lock is released.

System_CAPS_noteNote

If a thread has a reader lock, or no locks, calling ReleaseWriterLock throws an ApplicationException.

The following code example shows how to acquire and release a writer lock, and how to handle the exception thrown when a request times out.

This code is part of a larger example provided for the ReaderWriterLock class.

// 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;

// Shows how to request and release the writer lock, and
// how to handle time-outs.
static void WriteToResource( 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 );
   }

}


.NET Framework
Available since 1.1
Return to top
Show: