タイムアウトに Int32 値を使用して、ライタ ロックを取得します。
Overloads Public Sub AcquireWriterLock( _
ByVal millisecondsTimeout As Integer _)
[C#]
public void AcquireWriterLock(intmillisecondsTimeout);
[C++]
public: void AcquireWriterLock(intmillisecondsTimeout);
[JScript]
public function AcquireWriterLock(
millisecondsTimeout : int);
パラメータ
- millisecondsTimeout
ミリ秒単位のタイムアウト。
例外
例外の種類 | 条件 |
---|---|
ApplicationException | timeout は、ロック要求が許可される前に期限が切れます。 |
解説
別のスレッドがリーダー ロックまたはライタ ロックを保持している場合、このメソッドはブロックします。ライタ ロックと複数の同時リーダー ロックが交互に処理される方法の詳細については、 ReaderWriterLock クラスのトピックを参照してください。
既にリーダー ロックを保持しているスレッドがライタ ロックを取得するには、リーダー ロックを解放してから AcquireWriterLock を呼び出す方法と、 UpgradeToWriterLock を呼び出す方法があります。
注意 既にリーダー ロックを保持しているスレッドが AcquireWriterLock を呼び出した場合、既に保持しているリーダー ロックによってスレッドはブロックされます。タイムアウト値が無制限に設定されている場合、スレッドはデッドロック状態になります。このようなデッドロックを避けるには、 IsReaderLockHeld を使用して、現在のスレッドが既にリーダー ロックを保持していないかどうかを確認します。
AcquireWriterLock は、再帰的なライタ ロック要求をサポートします。つまり、1 つのスレッドは AcquireWriterLock を複数回呼び出すことができます。ロック カウントは呼び出しのたびにインクリメントされます。 ReleaseWriterLock は、 AcquireWriterLock を呼び出した回数だけ呼び出す必要があります。代わりに、 ReleaseLock を呼び出すことによって、ロック カウントをすぐに 0 に設定することもできます。
再帰的なロック要求は必ずすぐに許可されます。このとき、スレッドがライタ キューに置かれることはありません。
有効なタイムアウト値については、 ReaderWriterLock を参照してください。
使用例
' 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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
ReaderWriterLock クラス | ReaderWriterLock メンバ | System.Threading 名前空間 | ReaderWriterLock.AcquireWriterLock オーバーロードの一覧 | スレッド処理 | ReaderWriterLock