Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ReaderWriterLock.AcquireWriterLock Method (Int32)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Sub AcquireWriterLock( _
   ByVal millisecondsTimeout As Integer _
)
[C#]
public void AcquireWriterLock(
 int millisecondsTimeout
);
[C++]
public: void AcquireWriterLock(
 int millisecondsTimeout
);
[JScript]
public function AcquireWriterLock(
   millisecondsTimeout : int
);

Parameters

millisecondsTimeout
The time-out in milliseconds.

Exceptions

Exception Type Condition
ApplicationException timeout expires before the lock request is granted.

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.

CAUTION   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.

Example

[Visual Basic] 

' The complete code is located in the ReaderWriterLock
' class topic.
Imports System
Imports System.Threading
Imports Microsoft.VisualBasic

Public Class Test
    ' Declaring the ReaderWriterLock at the class level
    ' makes it visible to all threads.
    Private Shared rwl As New ReaderWriterLock()
    ' For this example, the shared resource protected by the
    ' ReaderWriterLock is just an integer.
    Private Shared resource As Integer = 0
    . . . 
    ' Shows how to request and release the writer lock, and
    ' how to handle time-outs.
    Shared Sub WriteToResource(timeOut As Integer)
        Try
            rwl.AcquireWriterLock(timeOut)
            Try
                ' It is 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.ReleaseWriterLock()
            End Try
        Catch ex As ApplicationException
            ' The writer lock request timed out.
            Interlocked.Increment(writerTimeouts)
        End Try
    End Sub 'WriteToResource
    . . . 
End Class 'Test 

[C#] 
// 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 the writer lock, and
    // how to handle time-outs.
    static void WriteToResource(int timeOut)
    {
        try
        {
            rwl.AcquireWriterLock(timeOut);
            try
            {
                // It is 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.ReleaseWriterLock();
            }
        }
        catch (ApplicationException)
        {
            // The writer lock request timed out.
            Interlocked.Increment(ref writerTimeouts);
        }
    }
    . . . 
}

[C++] 
// The complete code is located in the ReaderWriterLock
// class topic.
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

public __gc class Test 
{
public:
    // 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 the writer lock, and
    // how to handle time-outs.
    static void WriteToResource(int timeOut) 
    {
        try 
        {
            rwl->AcquireWriterLock(timeOut);
            try
            {
                // It is safe for this thread to read or write
                // from the shared resource.
                resource = rnd->Next(500);
                Display(String::Format(S"writes resource value {0}", __box(resource)));
                Interlocked::Increment(&writes);

            }        
            __finally 
            {
                // Ensure that the lock is released.
                rwl->ReleaseWriterLock();
            }
        } 
        catch (ApplicationException*) 
        {
            // The writer lock request timed out.
            Interlocked::Increment(&writerTimeouts);
        }
    }
    . . . 
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ReaderWriterLock Class | ReaderWriterLock Members | System.Threading Namespace | ReaderWriterLock.AcquireWriterLock Overload List | Threading | ReaderWriterLock

Show:
© 2014 Microsoft. All rights reserved.