次の方法で共有


ReaderWriterLock.RestoreLock メソッド

スレッドのロック ステータスを、 ReleaseLock を呼び出す前の状態に復元します。

Public Sub RestoreLock( _
   ByRef lockCookie As LockCookie _)
[C#]
public void RestoreLock(   ref LockCookielockCookie);
[C++]
public: void RestoreLock(LockCookie* lockCookie);
[JScript]
public function RestoreLock(
   lockCookie : LockCookie);

パラメータ

解説

RestoreLock 使用すると、再帰的なロック カウントも含めて状態が復元されます。

別のスレッドがライタ ロックを取得した後にリーダー ロックを復元しようとすると、または別のスレッドがリーダー ロックまたはライタ ロックを取得した後にライタ ロックを復元しようとすると、スレッドはブロックします。 RestoreLock はタイムアウト値を受け取らないため、デッドロックの可能性に十分注意してください。

注意    RestoreLock を呼び出すときは、 ReleaseLock の呼び出し以後に取得したロックがすべて解放済みであることを確認してください。たとえば、リーダー ロックを取得した後に以前のライタ ロックを復元しようとすると、スレッドはデッドロック状態になります。このような別のロックを検出するには、 IsReaderLockHeldIsWriterLockHeld を使用します。

UpgradeToWriterLock から返される LockCookie は使用しないでください。

使用例

 

' 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