Export (0) Print
Expand All

ReaderWriterLock.AcquireReaderLock Method (Int32)

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

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

public void AcquireReaderLock(
	int millisecondsTimeout
)

Parameters

millisecondsTimeout
Type: System.Int32

The time-out in milliseconds.

ExceptionCondition
ApplicationException

millisecondsTimeout expires before the lock request is granted.

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

NoteNote

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.

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 Test
{
    // Declaring the ReaderWriterLock at the class level 
    // makes it visible to all threads. 
    static ReaderWriterLock rwl = new ReaderWriterLock();
    // For this example, the shared resource protected by the 
    // ReaderWriterLock is just an integer. 
    static int resource = 0;


...


// Shows how to request and release a reader lock, and 
// how to 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);
    }
}


...


}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft