次の方法で共有


ReaderWriterLock.AnyWritersSince メソッド

シーケンス番号の取得後にライタ ロックを取得したスレッドがあったかどうかを示します。

Public Function AnyWritersSince( _
   ByVal seqNum As Integer _) As Boolean
[C#]
public bool AnyWritersSince(intseqNum);
[C++]
public: bool AnyWritersSince(intseqNum);
[JScript]
public function AnyWritersSince(
   seqNum : int) : Boolean;

パラメータ

  • seqNum
    シーケンス番号。

戻り値

シーケンス番号の取得後にライタ ロックを取得したスレッドがあった場合は true 。それ以外の場合は false

解説

WriterSeqNum および AnyWritersSince は、アプリケーションのパフォーマンス向上に役立てることができます。たとえば、リーダー ロックの保持中に取得した情報をキャッシュに格納しておきます。ロックを解放した後、再度リーダー ロックを取得した場合は、 AnyWritersSince を呼び出すことによって、別のスレッドがこのリソースに対して書き込みを行ったかどうかを確認します。書き込みを行ったリソースがなかった場合は、キャッシュに格納した情報を使用できます。この手法は、データベース クエリの実行など、ロックで保護された情報の読み取りの負荷が大きい場合に使用できます。

シーケンス番号を使用するときは、呼び出し元はリーダー ロックまたはライタ ロックを保持している必要があります。

使用例

 

' 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