ライタ ロックを取得します。
オーバーロードの一覧
タイムアウトに Int32 値を使用して、ライタ ロックを取得します。
[Visual Basic] Overloads Public Sub AcquireWriterLock(Integer)
[JScript] public function AcquireWriterLock(int);
タイムアウトに TimeSpan 値を使用して、ライタ ロックを取得します。
[Visual Basic] Overloads Public Sub AcquireWriterLock(TimeSpan)
使用例
[Visual Basic, C#, C++] メモ ここでは、AcquireWriterLock のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
' 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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
参照
ReaderWriterLock クラス | ReaderWriterLock メンバ | System.Threading 名前空間