ReaderWriterLock.UpgradeToWriterLock Method

Definition

Upgrades a reader lock to the writer lock.

Overloads

UpgradeToWriterLock(Int32)

Upgrades a reader lock to the writer lock, using an Int32 value for the time-out.

UpgradeToWriterLock(TimeSpan)

Upgrades a reader lock to the writer lock, using a TimeSpan value for the time-out.

UpgradeToWriterLock(Int32)

Upgrades a reader lock to the writer lock, using an Int32 value for the time-out.

public:
 System::Threading::LockCookie UpgradeToWriterLock(int millisecondsTimeout);
public System.Threading.LockCookie UpgradeToWriterLock (int millisecondsTimeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public System.Threading.LockCookie UpgradeToWriterLock (int millisecondsTimeout);
member this.UpgradeToWriterLock : int -> System.Threading.LockCookie
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.UpgradeToWriterLock : int -> System.Threading.LockCookie
Public Function UpgradeToWriterLock (millisecondsTimeout As Integer) As LockCookie

Parameters

millisecondsTimeout
Int32

The time-out in milliseconds.

Returns

A LockCookie value.

Attributes

Exceptions

millisecondsTimeout expires before the lock request is granted.

Examples

The following code example shows how to request a reader lock, upgrade the reader lock to a writer lock, and downgrade to a reader lock again.

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;
// 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 a reader lock, upgrade the
// reader lock to the writer lock, and downgrade to a
// reader lock again.
static void UpgradeDowngrade( Random^ rnd, 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 );

         // If it is necessary to write to the resource,
         // you must either release the reader lock and
         // then request the writer lock, or upgrade the
         // reader lock. Note that upgrading the reader lock
         // puts the thread in the write queue, behind any
         // other threads that might be waiting for the
         // writer lock.
         try
         {
            LockCookie lc = rwl->UpgradeToWriterLock( 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->DowngradeFromWriterLock( lc );
            }

         }
         catch ( ApplicationException^ )
         {

            // The upgrade request timed out.
            Interlocked::Increment( writerTimeouts );
         }


         // When the lock has been downgraded, it is
         // still safe to read from the 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 );
   }

}
// Requests a reader lock, upgrades the reader lock to the writer
// lock, and downgrades it to a reader lock again.
static void UpgradeDowngrade(Random rnd, int timeOut)
{
   try {
      rwl.AcquireReaderLock(timeOut);
      try {
         // It's safe for this thread to read from the shared resource.
         Display("reads resource value " + resource);
         Interlocked.Increment(ref reads);

         // To write to the resource, either release the reader lock and
         // request the writer lock, or upgrade the reader lock. Upgrading
         // the reader lock puts the thread in the write queue, behind any
         // other threads that might be waiting for the writer lock.
         try {
            LockCookie lc = rwl.UpgradeToWriterLock(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(ref writes);
            }
            finally {
               // Ensure that the lock is released.
               rwl.DowngradeFromWriterLock(ref lc);
            }
         }
         catch (ApplicationException) {
            // The upgrade request timed out.
            Interlocked.Increment(ref writerTimeouts);
         }

         // If the lock was downgraded, it's still safe to read from the resource.
         Display("reads resource value " + resource);
         Interlocked.Increment(ref reads);
      }
      finally {
         // Ensure that the lock is released.
         rwl.ReleaseReaderLock();
      }
   }
   catch (ApplicationException) {
      // The reader lock request timed out.
      Interlocked.Increment(ref readerTimeouts);
   }
}
' Requests a reader lock, upgrades the reader lock to the writer
' lock, and downgrades it to a reader lock again.
Sub UpgradeDowngrade(rnd As Random, timeOut As Integer)
   Try
      rwl.AcquireReaderLock(timeOut)
      Try
         ' It's safe for this thread to read from the shared resource.
         Display("reads resource value " & resource)
         Interlocked.Increment(reads)
         
         ' To write to the resource, either release the reader lock and
         ' request the writer lock, or upgrade the reader lock. Upgrading
         ' the reader lock puts the thread in the write queue, behind any
         ' other threads that might be waiting for the writer lock.
         Try
            Dim lc As LockCookie = rwl.UpgradeToWriterLock(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.DowngradeFromWriterLock(lc)
            End Try
         Catch ex As ApplicationException
            ' The upgrade request timed out.
            Interlocked.Increment(writerTimeouts)
         End Try
         
         ' If the lock was downgraded, it's still safe to read from the resource.
         Display("reads resource value " & resource)
         Interlocked.Increment(reads)
      Finally
         ' Ensure that the lock is released.
         rwl.ReleaseReaderLock()
      End Try
   Catch ex As ApplicationException
      ' The reader lock request timed out.
      Interlocked.Increment(readerTimeouts)
   End Try
End Sub
};
}
End Module

Remarks

When a thread calls UpgradeToWriterLock the reader lock is released, regardless of the lock count, and the thread goes to the end of the queue for the writer lock. Thus, other threads might write to the resource before the thread that requested the upgrade is granted the writer lock.

Important

The time-out exception is not thrown until the thread that called the UpgradeToWriterLock method can reacquire the reader lock. If there are no other threads waiting for the writer lock, this happens immediately. However, if another thread is queued for the writer lock, the thread that called the UpgradeToWriterLock method cannot reacquire the reader lock until all current readers have released their locks, and one thread has acquired and released the writer lock. This is true even if the other thread that requested the writer lock requested it after the current thread called the UpgradeToWriterLock method.

To restore the lock state, call DowngradeFromWriterLock using the LockCookie returned by UpgradeToWriterLock. Do not use this LockCookie with RestoreLock.

When a thread has no reader lock, do not use UpgradeToWriterLock. Use AcquireWriterLock instead.

For valid time-out values, see ReaderWriterLock.

See also

Applies to

UpgradeToWriterLock(TimeSpan)

Upgrades a reader lock to the writer lock, using a TimeSpan value for the time-out.

public:
 System::Threading::LockCookie UpgradeToWriterLock(TimeSpan timeout);
public System.Threading.LockCookie UpgradeToWriterLock (TimeSpan timeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public System.Threading.LockCookie UpgradeToWriterLock (TimeSpan timeout);
member this.UpgradeToWriterLock : TimeSpan -> System.Threading.LockCookie
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.UpgradeToWriterLock : TimeSpan -> System.Threading.LockCookie
Public Function UpgradeToWriterLock (timeout As TimeSpan) As LockCookie

Parameters

timeout
TimeSpan

The TimeSpan specifying the time-out period.

Returns

A LockCookie value.

Attributes

Exceptions

timeout expires before the lock request is granted.

timeout specifies a negative value other than -1 milliseconds.

Remarks

When a thread calls UpgradeToWriterLock the reader lock is released, regardless of the lock count, and the thread goes to the end of the queue for the writer lock. Thus, other threads might write to the resource before the thread that requested the upgrade is granted the writer lock.

Important

The time-out exception is not thrown until the thread that called the UpgradeToWriterLock method can reacquire the reader lock. If there are no other threads waiting for the writer lock, this happens immediately. However, if another thread is queued for the writer lock, the thread that called the UpgradeToWriterLock method cannot reacquire the reader lock until all current readers have released their locks, and one thread has acquired and released the writer lock. This is true even if the other thread that requested the writer lock requested it after the current thread called the UpgradeToWriterLock method.

To restore the lock state, call DowngradeFromWriterLock using the LockCookie returned by UpgradeToWriterLock. Do not use this LockCookie with RestoreLock.

When a thread has no reader lock, do not use UpgradeToWriterLock. Use AcquireWriterLock instead.

For valid time-out values, see ReaderWriterLock.

See also

Applies to