ReaderWriterLock.AcquireReaderLock Method

Definition

Acquires a reader lock.

Overloads

AcquireReaderLock(Int32)

Acquires a reader lock, using an Int32 value for the time-out.

AcquireReaderLock(TimeSpan)

Acquires a reader lock, using a TimeSpan value for the time-out.

AcquireReaderLock(Int32)

Source:
ReaderWriterLock.cs
Source:
ReaderWriterLock.cs
Source:
ReaderWriterLock.cs

Acquires a reader lock, using an Int32 value for the time-out.

[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void AcquireReaderLock(int millisecondsTimeout);
public void AcquireReaderLock(int millisecondsTimeout);

Parameters

millisecondsTimeout
Int32

The time-out in milliseconds.

Attributes

Exceptions

millisecondsTimeout expires before the lock request is granted.

Examples

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 System;
using System.Threading;

public class Example
{
   static ReaderWriterLock rwl = new ReaderWriterLock();
   // Define the shared resource protected by the ReaderWriterLock.
   static int resource = 0;
// Request and release a reader lock, and 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("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);
   }
}
}

Remarks

AcquireReaderLock blocks if a different thread has the writer lock, or if at least one thread is waiting for the writer lock.

Poznámka

If the current thread already has the writer lock, no reader lock is acquired. Instead, the lock count on the writer lock is incremented. This prevents a thread from blocking on its own writer lock. The result is exactly the same as calling AcquireWriterLock, and an additional call to ReleaseWriterLock is required when releasing the writer lock.

AcquireReaderLock supports recursive reader-lock requests. That is, a thread can call AcquireReaderLock multiple times, which increments the lock count each time. You must call ReleaseReaderLock once for each time you call AcquireReaderLock. Alternatively, you can call ReleaseLock to reduce the lock count to zero immediately.

Recursive lock requests are always granted immediately, without placing the requesting thread in the reader queue. Use recursive locks with caution, to avoid blocking writer-lock requests for long periods.

For valid time-out values, see ReaderWriterLock.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.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

AcquireReaderLock(TimeSpan)

Source:
ReaderWriterLock.cs
Source:
ReaderWriterLock.cs
Source:
ReaderWriterLock.cs

Acquires a reader lock, using a TimeSpan value for the time-out.

[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void AcquireReaderLock(TimeSpan timeout);
public void AcquireReaderLock(TimeSpan timeout);

Parameters

timeout
TimeSpan

A TimeSpan specifying the time-out period.

Attributes

Exceptions

timeout expires before the lock request is granted.

timeout specifies a negative value other than -1 milliseconds.

Remarks

AcquireReaderLock blocks if a different thread has the writer lock, or if at least one thread is waiting for the writer lock.

Poznámka

If the current thread already has the writer lock, no reader lock is acquired. Instead, the lock count on the writer lock is incremented. This prevents a thread from blocking on its own writer lock. The result is exactly the same as calling AcquireWriterLock, and an additional call to ReleaseWriterLock is required when releasing the writer lock.

AcquireReaderLock supports recursive reader-lock requests. That is, a thread can call AcquireReaderLock multiple times, which increments the lock count each time. You must call ReleaseReaderLock once for each time you call AcquireReaderLock. Alternatively, you can call ReleaseLock to reduce the lock count to zero immediately.

Recursive lock requests are always granted immediately, without placing the requesting thread in the reader queue. Use recursive locks with caution, to avoid blocking writer-lock requests for long periods.

For valid time-out values, see ReaderWriterLock.

See also

Applies to

.NET 10 a další verze
Produkt Verze
.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