ReaderWriterLock.AcquireWriterLock Method

Definition

Acquires the writer lock.

Overloads

AcquireWriterLock(Int32)

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

AcquireWriterLock(TimeSpan)

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

AcquireWriterLock(Int32)

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

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

public void AcquireWriterLock (int millisecondsTimeout);

Parameters

millisecondsTimeout
Int32

The time-out in milliseconds.

Exceptions

timeout expires before the lock request is granted.

Examples

The following code example shows how to acquire and release a writer 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 the writer lock, and handle time-outs.
static void WriteToResource(Random rnd, int timeOut)
{
   try {
      rwl.AcquireWriterLock(timeOut);
      try {
         // It's safe for this thread to access 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.ReleaseWriterLock();
      }
   }
   catch (ApplicationException) {
      // The writer lock request timed out.
      Interlocked.Increment(ref writerTimeouts);
   }
}
}

Remarks

This method blocks if another thread has a reader lock or writer lock. For a description of the way the writer lock alternates with multiple concurrent reader locks, see the ReaderWriterLock class.

A thread that already has a reader lock can acquire the writer lock in one of two ways: by releasing the reader lock before calling AcquireWriterLock, or by calling UpgradeToWriterLock.

주의

If a thread calls AcquireWriterLock while it still has a reader lock, it will block on its own reader lock; if an infinite time-out is specified, the thread will deadlock. To avoid such deadlocks, use IsReaderLockHeld to determine whether the current thread already has a reader lock.

AcquireWriterLock supports recursive writer-lock requests. That is, a thread can call AcquireWriterLock multiple times, which increments the lock count each time. You must call ReleaseWriterLock once for each time you call AcquireWriterLock. 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 writer queue.

For valid time-out values, see ReaderWriterLock.

See also

Applies to

.NET 9 및 기타 버전
제품 버전
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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

AcquireWriterLock(TimeSpan)

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

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

public void AcquireWriterLock (TimeSpan timeout);

Parameters

timeout
TimeSpan

The TimeSpan specifying the time-out period.

Exceptions

timeout expires before the lock request is granted.

timeout specifies a negative value other than -1 milliseconds.

Remarks

This method blocks if another thread has a reader lock or writer lock. For a description of the way the writer lock alternates with multiple concurrent reader locks, see the ReaderWriterLock class.

A thread that already has a reader lock can acquire the writer lock in one of two ways: by releasing the reader lock before calling AcquireWriterLock, or by calling UpgradeToWriterLock.

주의

If a thread calls AcquireWriterLock while it still has a reader lock, it will block on its own reader lock; if an infinite time-out is specified, the thread will deadlock. To avoid such deadlocks, use IsReaderLockHeld to determine whether the current thread already has a reader lock.

AcquireWriterLock supports recursive writer-lock requests. That is, a thread can call AcquireWriterLock multiple times, which increments the lock count each time. You must call ReleaseWriterLock once for each time you call AcquireWriterLock. 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 writer queue.

For valid time-out values, see ReaderWriterLock.

See also

Applies to

.NET 9 및 기타 버전
제품 버전
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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