ReaderWriterLock.WriterSeqNum Property

Definition

Gets the current sequence number.

C#
public int WriterSeqNum { get; }

Property Value

The current sequence number.

Examples

The following code example shows how to use the WriterSeqNum property and the AnyWritersSince method to determine whether another thread acquired the writer lock on the protected resource since the current thread last held the writer lock.

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

C#
// 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;
C#
// 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(Random rnd, 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);
   }
}
C#
}

Remarks

The sequence number increases whenever a thread acquires the writer lock. You can save the sequence number and pass it to AnyWritersSince at a later time, if you want to determine whether other threads have acquired the writer lock in the meantime.

You can use WriterSeqNum to improve application performance. For example, a thread might cache the information it obtains while holding a reader lock. After releasing and later reacquiring the lock, the thread can determine whether other threads have written to the resource by calling AnyWritersSince; if not, the cached information can be used. This technique is useful when reading the information protected by the lock is expensive; for example, running a database query.

The caller must be holding a reader lock or a writer lock in order for the sequence number to be useful.

Applies to

Tuote Versiot
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also