タイムアウトに Int32 値を使用して、リーダー ロックを取得します。
Overloads Public Sub AcquireReaderLock( _
ByVal millisecondsTimeout As Integer _)
[C#]
public void AcquireReaderLock(intmillisecondsTimeout);
[C++]
public: void AcquireReaderLock(intmillisecondsTimeout);
[JScript]
public function AcquireReaderLock(
millisecondsTimeout : int);
パラメータ
- millisecondsTimeout
ミリ秒単位のタイムアウト。
例外
例外の種類 | 条件 |
---|---|
ApplicationException | timeout は、ロック要求が許可される前に期限が切れます。 |
解説
別のスレッドがライタ ロックを保持している場合、または 1 つ以上のスレッドがライタ ロックを待機している場合、 AcquireReaderLock はブロックします。
メモ 現在のスレッドがライタ ロックを既に保持している場合、リーダー ロックは取得されません。代わりに、ライタ ロックのロック カウントがインクリメントされます。これによって、スレッドが自身のライタ ロックでブロックすることを防ぐことができます。この結果は AcquireWriterLock を呼び出した場合とまったく同じです。この場合、ライタ ロックを解放するには、追加の ReleaseWriterLock 呼び出しが必要となります。
AcquireReaderLock は、再帰的なリーダー ロック要求をサポートします。つまり、1 つのスレッドは AcquireReaderLock を複数回呼び出すことができます。ロック カウントは呼び出しのたびにインクリメントされます。 ReleaseReaderLock は、 AcquireReaderLock を呼び出した回数だけ呼び出す必要があります。代わりに、 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 a reader lock, and
' how to handle time-outs.
Shared Sub ReadFromResource(timeOut As Integer)
Try
rwl.AcquireReaderLock(timeOut)
Try
' It is safe for this thread to read from
' the shared resource.
Display("reads resource value " & resource)
Interlocked.Increment(reads)
Finally
' Ensure that the lock is released.
rwl.ReleaseReaderLock()
End Try
Catch ex As ApplicationException
' The reader lock request timed out.
Interlocked.Increment(readerTimeouts)
End Try
End Sub 'ReadFromResource
. . .
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 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);
}
}
. . .
}
[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 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(String::Format(S"reads resource value {0}", __box(resource)));
Interlocked::Increment(&reads);
}
__finally
{
// Ensure that the lock is released.
rwl->ReleaseReaderLock();
}
}
catch (ApplicationException*)
{
// The reader lock request timed out.
Interlocked::Increment(&readerTimeouts);
}
}
. . .
};
[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.AcquireReaderLock オーバーロードの一覧 | スレッド処理 | ReaderWriterLock