次の方法で共有


ReaderWriterLock.AcquireReaderLock メソッド

リーダー ロックを取得します。

オーバーロードの一覧

タイムアウトに Int32 値を使用して、リーダー ロックを取得します。

[Visual Basic] Overloads Public Sub AcquireReaderLock(Integer)

[C#] public void AcquireReaderLock(int);

[C++] public: void AcquireReaderLock(int);

[JScript] public function AcquireReaderLock(int);

タイムアウトに TimeSpan 値を使用して、リーダー ロックを取得します。

[Visual Basic] Overloads Public Sub AcquireReaderLock(TimeSpan)

[C#] public void AcquireReaderLock(TimeSpan);

[C++] public: void AcquireReaderLock(TimeSpan);

[JScript] public function AcquireReaderLock(TimeSpan);

使用例

[Visual Basic, C#, C++] メモ   ここでは、AcquireReaderLock のオーバーロード形式のうちの 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 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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

ReaderWriterLock クラス | ReaderWriterLock メンバ | System.Threading 名前空間