ReaderWriterLock.AcquireWriterLock 方法

定义

获取写线程锁。

重载

AcquireWriterLock(Int32)

使用一个 Int32 超时值获取写线程锁。

AcquireWriterLock(TimeSpan)

使用一个 TimeSpan 超时值获取写线程锁。

AcquireWriterLock(Int32)

Source:
ReaderWriterLock.cs
Source:
ReaderWriterLock.cs
Source:
ReaderWriterLock.cs

使用一个 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 ,这每次都会递增锁计数。 每次调用 ReleaseWriterLock 时,必须调用 AcquireWriterLock一次。 或者,可以调用 ReleaseLock 将锁计数立即减少到零。

递归锁请求始终立即授予,而无需将请求线程置于编写器队列中。

有关有效的超时值,请参阅 ReaderWriterLock

另请参阅

适用于

AcquireWriterLock(TimeSpan)

Source:
ReaderWriterLock.cs
Source:
ReaderWriterLock.cs
Source:
ReaderWriterLock.cs

使用一个 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 ,这每次都会递增锁计数。 每次调用 ReleaseWriterLock 时,必须调用 AcquireWriterLock一次。 或者,可以调用 ReleaseLock 将锁计数立即减少到零。

递归锁请求始终立即授予,而无需将请求线程置于编写器队列中。

有关有效的超时值,请参阅 ReaderWriterLock

另请参阅

适用于