共用方式為


ReaderWriterLock.AcquireReaderLock 方法

定義

取得讀取器的鎖定。

多載

AcquireReaderLock(Int32)

使用逾時值 Int32 取得讀取器的鎖定。

AcquireReaderLock(TimeSpan)

使用逾時值 TimeSpan 取得讀取器的鎖定。

AcquireReaderLock(Int32)

來源:
ReaderWriterLock.cs
來源:
ReaderWriterLock.cs
來源:
ReaderWriterLock.cs

使用逾時值 Int32 取得讀取器的鎖定。

public:
 void AcquireReaderLock(int millisecondsTimeout);
public void AcquireReaderLock (int millisecondsTimeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void AcquireReaderLock (int millisecondsTimeout);
member this.AcquireReaderLock : int -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.AcquireReaderLock : int -> unit
Public Sub AcquireReaderLock (millisecondsTimeout As Integer)

參數

millisecondsTimeout
Int32

逾時以毫秒為單位。

屬性

例外狀況

millisecondsTimeout 在授與鎖定要求前過期。

範例

下列程式碼範例示範如何取得和釋放讀取器鎖定,以及如何處理在要求逾時時擲回的例外狀況。

此程式碼是類別所提供較大範例的 ReaderWriterLock 一部分。

// The complete code is located in the ReaderWriterLock
// class topic.
using namespace System;
using namespace System::Threading;
public ref class Test
{
public:

   // Declaring the ReaderWriterLock at the class level
   // makes it visible to all threads.
   static ReaderWriterLock^ rwl = gcnew ReaderWriterLock;

   // For this example, the shared resource protected by the
   // ReaderWriterLock is just an integer.
   static int resource = 0;
// The complete code is located in the ReaderWriterLock class topic.
using System;
using System.Threading;

public class Example
{
   static ReaderWriterLock rwl = new ReaderWriterLock();
   // Define the shared resource protected by the ReaderWriterLock.
   static int resource = 0;
' The complete code is located in the ReaderWriterLock class topic.
Imports System.Threading

Public Module Example
   Private rwl As New ReaderWriterLock()
   ' Define the shared resource protected by the ReaderWriterLock.
   Private resource As Integer = 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( "reads resource value {0}", resource ) );
         Interlocked::Increment( reads );
      }
      finally
      {

         // Ensure that the lock is released.
         rwl->ReleaseReaderLock();
      }

   }
   catch ( ApplicationException^ )
   {

      // The reader lock request timed out.
      Interlocked::Increment( readerTimeouts );
   }

}
// Request and release a reader lock, and 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);
   }
}
' Request and release a reader lock, and handle time-outs.
Sub ReadFromResource(timeOut As Integer)
   Try
      rwl.AcquireReaderLock(timeOut)
      Try
         ' It's 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
};
}
End Module

備註

AcquireReaderLock 如果不同的執行緒具有寫入器鎖定,或至少一個執行緒正在等候寫入器鎖定,則會封鎖 。

注意

如果目前的執行緒已經有寫入器鎖定,則不會取得讀取器鎖定。 相反地,寫入器鎖定上的鎖定計數會遞增。 這可防止執行緒在其自己的寫入器鎖定上封鎖。 結果與呼叫 AcquireWriterLock 完全相同,而且在釋放寫入器鎖定時需要額外的 呼叫 ReleaseWriterLock

AcquireReaderLock 支援遞迴讀取器鎖定要求。 也就是說,執行緒可以多次呼叫 AcquireReaderLock,每次都會遞增鎖定計數。 每次呼叫 時,都必須呼叫 ReleaseReaderLockAcquireReaderLock 一次。 或者,您可以呼叫 ReleaseLock 來立即將鎖定計數減少為零。

遞迴鎖定要求一律會立即授與,而不會在讀取器佇列中放置要求執行緒。 請小心使用遞迴鎖定,以避免長時間封鎖寫入器鎖定要求。

如需有效的逾時值,請參閱 ReaderWriterLock

另請參閱

適用於

AcquireReaderLock(TimeSpan)

來源:
ReaderWriterLock.cs
來源:
ReaderWriterLock.cs
來源:
ReaderWriterLock.cs

使用逾時值 TimeSpan 取得讀取器的鎖定。

public:
 void AcquireReaderLock(TimeSpan timeout);
public void AcquireReaderLock (TimeSpan timeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void AcquireReaderLock (TimeSpan timeout);
member this.AcquireReaderLock : TimeSpan -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.AcquireReaderLock : TimeSpan -> unit
Public Sub AcquireReaderLock (timeout As TimeSpan)

參數

timeout
TimeSpan

TimeSpan,指定逾時期間。

屬性

例外狀況

timeout 在授與鎖定要求前過期。

timeout 可以指定 -1 毫秒以外的負值。

備註

AcquireReaderLock 如果不同的執行緒具有寫入器鎖定,或至少一個執行緒正在等候寫入器鎖定,則會封鎖 。

注意

如果目前的執行緒已經有寫入器鎖定,則不會取得讀取器鎖定。 相反地,寫入器鎖定上的鎖定計數會遞增。 這可防止執行緒在其自己的寫入器鎖定上封鎖。 結果與呼叫 AcquireWriterLock 完全相同,而且在釋放寫入器鎖定時需要額外的 呼叫 ReleaseWriterLock

AcquireReaderLock 支援遞迴讀取器鎖定要求。 也就是說,執行緒可以多次呼叫 AcquireReaderLock,每次都會遞增鎖定計數。 每次呼叫 時,都必須呼叫 ReleaseReaderLockAcquireReaderLock 一次。 或者,您可以呼叫 ReleaseLock 來立即將鎖定計數減少為零。

遞迴鎖定要求一律會立即授與,而不會在讀取器佇列中放置要求執行緒。 請小心使用遞迴鎖定,以避免長時間封鎖寫入器鎖定要求。

如需有效的逾時值,請參閱 ReaderWriterLock

另請參閱

適用於