ReaderWriterLock::ReleaseReaderLock Method ()

 

Decrements the lock count.

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

public:
void ReleaseReaderLock()

Exception Condition
ApplicationException

The thread does not have any reader or writer locks.

ReleaseReaderLock decrements the lock count. When the count reaches zero, the lock is released.

System_CAPS_noteNote

If a thread has the writer lock, calling ReleaseReaderLock has the same effect as calling ReleaseWriterLock. If a thread has no locks, calling ReleaseReaderLock throws an ApplicationException.

The following code example shows how to acquire and release a reader 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 a reader lock, and
// how to handle time-outs.
static void ReadFromResource( int timeOut )
{
   try
   {
      rwl->AcquireReaderLock( timeOut );
      try
      {

         // It is safe for this thread to read from
         // the shared resource.
         Display( String::Format( "reads resource value {0}", resource ) );
         Interlocked::Increment( reads );
      }
      finally
      {

         // Ensure that the lock is released.
         rwl->ReleaseReaderLock();
      }

   }
   catch ( ApplicationException^ ) 
   {

      // The reader lock request timed out.
      Interlocked::Increment( readerTimeouts );
   }

}


.NET Framework
Available since 1.1
Return to top
Show: