共用方式為


ReaderWriterLock.AcquireWriterLock 方法

定義

取得寫入器的鎖定。

多載

AcquireWriterLock(Int32)

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

AcquireWriterLock(TimeSpan)

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

AcquireWriterLock(Int32)

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

public:
 void AcquireWriterLock(int millisecondsTimeout);
public void AcquireWriterLock (int millisecondsTimeout);
member this.AcquireWriterLock : int -> unit
Public Sub AcquireWriterLock (millisecondsTimeout As Integer)

參數

millisecondsTimeout
Int32

逾時以毫秒為單位。

例外狀況

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

範例

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

此程式碼是提供給 類別之較大範例的 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 the writer lock, and
// how to handle time-outs.
static void WriteToResource( Random^ rnd, int timeOut )
{
   try
   {
      rwl->AcquireWriterLock( timeOut );
      try
      {

         // It is safe for this thread to read or write
         // from the shared resource.
         resource = rnd->Next( 500 );
         Display( String::Format( "writes resource value {0}", resource ) );
         Interlocked::Increment( writes );
      }
      finally
      {

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

   }
   catch ( ApplicationException^ )
   {

      // The writer lock request timed out.
      Interlocked::Increment( writerTimeouts );
   }

}
// Request and release the writer lock, and handle time-outs.
static void WriteToResource(Random rnd, int timeOut)
{
   try {
      rwl.AcquireWriterLock(timeOut);
      try {
         // It's safe for this thread to access from the shared resource.
         resource = rnd.Next(500);
         Display("writes resource value " + resource);
         Interlocked.Increment(ref writes);
      }
      finally {
         // Ensure that the lock is released.
         rwl.ReleaseWriterLock();
      }
   }
   catch (ApplicationException) {
      // The writer lock request timed out.
      Interlocked.Increment(ref writerTimeouts);
   }
}
' Request and release the writer lock, and handle time-outs.
Sub WriteToResource(rnd As Random, timeOut As Integer)
   Try
      rwl.AcquireWriterLock(timeOut)
      Try
         ' It's safe for this thread to read or write from the shared resource.
         resource = rnd.Next(500)
         Display("writes resource value " & resource)
         Interlocked.Increment(writes)
      Finally
         ' Ensure that the lock is released.
         rwl.ReleaseWriterLock()
      End Try
   Catch ex As ApplicationException
      ' The writer lock request timed out.
      Interlocked.Increment(writerTimeouts)
   End Try
End Sub
};
}
End Module

備註

這個方法會封鎖另一個執行緒是否有讀取器鎖定或寫入器鎖定。 如需寫入器鎖定與多個並行讀取器鎖定替代方式的描述,請參閱 ReaderWriterLock 類別。

已經有讀取器鎖定的執行緒可以透過下列兩種方式之一取得寫入器鎖定:在呼叫 AcquireWriterLock 之前釋放讀取器鎖定,或呼叫 UpgradeToWriterLock

警告

如果執行緒在仍然有讀取器鎖定時呼叫 AcquireWriterLock ,它會在自己的讀取器鎖定上封鎖;如果指定無限逾時,執行緒將會死結。 若要避免這類死結,請使用 IsReaderLockHeld 來判斷目前的執行緒是否已經有讀取器鎖定。

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

遞迴鎖定要求一律會立即授與,而不需要將要求執行緒放在寫入器佇列中。

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

另請參閱

適用於

AcquireWriterLock(TimeSpan)

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

public:
 void AcquireWriterLock(TimeSpan timeout);
public void AcquireWriterLock (TimeSpan timeout);
member this.AcquireWriterLock : TimeSpan -> unit
Public Sub AcquireWriterLock (timeout As TimeSpan)

參數

timeout
TimeSpan

TimeSpan,指定逾時期間。

例外狀況

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

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

備註

如果另一個執行緒有讀取器鎖定或寫入器鎖定,這個方法會封鎖。 如需寫入器鎖定與多個並行讀取器鎖定替代方式的描述,請參閱 類別 ReaderWriterLock

已經有讀取器鎖定的執行緒可以透過下列兩種方式之一取得寫入器鎖定:在呼叫 AcquireWriterLock 之前釋放讀取器鎖定,或藉由呼叫 UpgradeToWriterLock

警告

如果執行緒在仍然有讀取器鎖定時呼叫 AcquireWriterLock ,則會封鎖自己的讀取器鎖定;如果指定無限逾時,執行緒將會死結。 若要避免這類死結,請使用 IsReaderLockHeld 來判斷目前的執行緒是否已經有讀取器鎖定。

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

遞迴鎖定要求一律會立即授與,而不需要將要求執行緒放在寫入器佇列中。

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

另請參閱

適用於