スレッドがロックを取得した回数に関係なく、ロックを解放します。
Public Function ReleaseLock() As LockCookie
[C#]
public LockCookie ReleaseLock();
[C++]
public: LockCookie ReleaseLock();
[JScript]
public function ReleaseLock() : LockCookie;
戻り値
解放されたロックを表す LockCookie 値。
解説
ReleaseLock を呼び出すと、再帰的なロック カウントに関係なく、リーダー ロックまたはライタ ロックが解放されます。ロック カウントも含め、ロックの状態を復元するには LockCookie を RestoreLock に渡します。
使用例
' 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 release all locks and later restore
' the lock state. Shows how to use sequence numbers
' to determine whether another thread has obtained
' a writer lock since this thread last accessed the
' resource.
Shared Sub ReleaseRestore(timeOut As Integer)
Dim lastWriter As Integer
Try
rwl.AcquireReaderLock(timeOut)
Try
' It is safe for this thread to read from
' the shared resource. Cache the value. (You
' might do this if reading the resource is
' an expensive operation.)
Dim resourceValue As Integer = resource
Display("reads resource value " & resourceValue)
Interlocked.Increment(reads)
' Save the current writer sequence number.
lastWriter = rwl.WriterSeqNum
' Release the lock, and save a cookie so the
' lock can be restored later.
Dim lc As LockCookie = rwl.ReleaseLock()
' Wait for a random interval (up to a
' quarter of a second), and then restore
' the previous state of the lock. Note that
' there is no time-out on the Restore method.
Thread.Sleep(rnd.Next(250))
rwl.RestoreLock(lc)
' Check whether other threads obtained the
' writer lock in the interval. If not, then
' the cached value of the resource is still
' valid.
If rwl.AnyWritersSince(lastWriter) Then
resourceValue = resource
Interlocked.Increment(reads)
Display("resource has changed " & resourceValue)
Else
Display("resource has not changed " & resourceValue)
End If
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 'ReleaseRestore
. . .
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 release all locks and later restore
// the lock state. Shows how to use sequence numbers
// to determine whether another thread has obtained
// a writer lock since this thread last accessed the
// resource.
static void ReleaseRestore(int timeOut)
{
int lastWriter;
try
{
rwl.AcquireReaderLock(timeOut);
try
{
// It is safe for this thread to read from
// the shared resource. Cache the value. (You
// might do this if reading the resource is
// an expensive operation.)
int resourceValue = resource;
Display("reads resource value " + resourceValue);
Interlocked.Increment(ref reads);
// Save the current writer sequence number.
lastWriter = rwl.WriterSeqNum;
// Release the lock, and save a cookie so the
// lock can be restored later.
LockCookie lc = rwl.ReleaseLock();
// Wait for a random interval (up to a
// quarter of a second), and then restore
// the previous state of the lock. Note that
// there is no time-out on the Restore method.
Thread.Sleep(rnd.Next(250));
rwl.RestoreLock(ref lc);
// Check whether other threads obtained the
// writer lock in the interval. If not, then
// the cached value of the resource is still
// valid.
if (rwl.AnyWritersSince(lastWriter))
{
resourceValue = resource;
Interlocked.Increment(ref reads);
Display("resource has changed " + resourceValue);
}
else
{
Display("resource has not changed " + resourceValue);
}
}
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 release all locks and later restore
// the lock state. Shows how to use sequence numbers
// to determine whether another thread has obtained
// a writer lock since this thread last accessed the
// resource.
static void ReleaseRestore(int timeOut)
{
int lastWriter;
try
{
rwl->AcquireReaderLock(timeOut);
try
{
// It is safe for this thread to read from
// the shared resource. Cache the value. (You
// might do this if reading the resource is
// an expensive operation.)
int resourceValue = resource;
Display(String::Format(S"reads resource value {0}", __box(resourceValue)));
Interlocked::Increment(&reads);
// Save the current writer sequence number.
lastWriter = rwl->WriterSeqNum;
// Release the lock, and save a cookie so the
// lock can be restored later.
LockCookie lc = rwl->ReleaseLock();
// Wait for a random interval (up to a
// quarter of a second), and then restore
// the previous state of the lock. Note that
// there is no time-->Item[Out] on* the Restore method.
Thread::Sleep(rnd->Next(250));
rwl->RestoreLock(__box(lc));
// Check whether other threads obtained the
// writer lock in the interval. If not, then
// the cached value of the resource is still
// valid.
if (rwl->AnyWritersSince(lastWriter))
{
resourceValue = resource;
Interlocked::Increment(&reads);
Display(String::Format(S"resource has changed {0}", __box(resourceValue)));
}
else
{
Display(String::Format(S"resource has not changed {0}", __box(resourceValue)));
}
}
__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