ReaderWriterLock.RestoreLock Method (LockCookie)

 

Restores the lock status of the thread to what it was before calling ReleaseLock.

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

public void RestoreLock(
	ref LockCookie lockCookie
)

Parameters

lockCookie
Type: System.Threading.LockCookie

A LockCookie returned by ReleaseLock.

Exception Condition
NullReferenceException

The address of lockCookie is a null pointer.

The state restored by RestoreLock includes the recursive lock count.

A thread blocks if it tries to restore a reader lock after another thread has acquired the writer lock, or if it tries to restore the writer lock after another thread has acquired a reader lock or writer lock. Because RestoreLock does not accept a time-out, you should take care to avoid possible deadlocks.

System_CAPS_cautionCaution

Before calling RestoreLock, make sure you have released all locks acquired since the call to ReleaseLock. For example, a thread deadlocks if it acquires a reader lock, and then attempts to restore an earlier writer lock. Use IsReaderLockHeld and IsWriterLockHeld to detect such additional locks.

Do not use a LockCookie returned from UpgradeToWriterLock.

The following code example shows how to use the ReleaseLock method to release the lock, regardless of how many times it has been acquired by the thread, and how to restore the state of the lock later.

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

// 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;
// Release all locks and later restores the lock state.
// Uses sequence numbers to determine whether another thread has
// obtained a writer lock since this thread last accessed the resource.
static void ReleaseRestore(int timeOut)
{
   int lastWriter;

   try {
      rwl.AcquireReaderLock(timeOut);
      try {
         // It's safe for this thread to read from the shared resource,
         // so read and cache the resource value.
         int resourceValue = resource;     // Cache the resource value.
         Display("reads resource value " + resourceValue);
         Interlocked.Increment(ref reads);

         // Save the current writer sequence number.
         lastWriter = rwl.WriterSeqNum;

         // Release the lock and save a cookie so the lock can be restored later.
         LockCookie lc = rwl.ReleaseLock();

         // Wait for a random interval and then restore the previous state of the lock.
         Thread.Sleep(rnd.Next(250));
         rwl.RestoreLock(ref lc);

         // Check whether other threads obtained the writer lock in the interval.
         // If not, then the cached value of the resource is still valid.
         if (rwl.AnyWritersSince(lastWriter)) {
            resourceValue = resource;
            Interlocked.Increment(ref reads);
            Display("resource has changed " + resourceValue);
         }
         else {
            Display("resource has not changed " + resourceValue);
         }
      }
      finally {
         // Ensure that the lock is released.
         rwl.ReleaseReaderLock();
      }
   }
   catch (ApplicationException) {
      // The reader lock request timed out.
      Interlocked.Increment(ref readerTimeouts);
   }
}

.NET Framework
Available since 1.1
Return to top
Show: