Share via


ReaderWriterLock 类

定义支持单个写线程和多个读线程的锁。

**命名空间:**System.Threading
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
Public NotInheritable Class ReaderWriterLock
    Inherits CriticalFinalizerObject
用法
Dim instance As ReaderWriterLock
[ComVisibleAttribute(true)] 
public sealed class ReaderWriterLock : CriticalFinalizerObject
[ComVisibleAttribute(true)] 
public ref class ReaderWriterLock sealed : public CriticalFinalizerObject
/** @attribute ComVisibleAttribute(true) */ 
public final class ReaderWriterLock extends CriticalFinalizerObject
ComVisibleAttribute(true) 
public final class ReaderWriterLock extends CriticalFinalizerObject

备注

提示

应用于此类的 HostProtectionAttribute 属性 (Attribute) 具有以下 Resources 属性 (Property) 值:Synchronization | ExternalThreadingHostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标,键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性

ReaderWriterLock 用于同步对资源的访问。在任一特定时刻,它允许多个线程同时进行读访问,或者允许单个线程进行写访问。在资源不经常发生更改的情况下,ReaderWriterLock 所提供的吞吐量比简单的一次只允许一个线程的锁(如 Monitor)更高。

在多数访问为读访问,而写访问频率较低、持续时间也比较短的情况下,ReaderWriterLock 的性能最好。多个读线程与单个写线程交替进行操作,所以读线程和写线程都不会长时间阻止。

提示

长时间持有读线程锁或写线程锁会使其他线程发生饥饿 (starve)。为了得到最好的性能,需要考虑重新构造应用程序以将写访问的持续时间减少到最小。

一个线程可以持有读线程锁或写线程锁,但是不能同时持有两者。若要获取写线程锁,请使用 UpgradeToWriterLockDowngradeFromWriterLock,而不要通过释放读线程锁的方式获取。

递归锁请求会增加锁上的锁计数。

读线程和写线程将分别排入各自的队列。当线程释放写线程锁时,此刻读线程队列中的所有等待线程都将被授予读线程锁;当已释放所有读线程锁时,写线程队列中处于等待状态的下一个线程(如果存在)将被授予写线程锁,依此类推。换句话说,ReaderWriterLock 在一组读线程和一个写线程之间交替进行操作。

当写线程队列中有一个线程在等待活动读线程锁被释放时,请求新的读线程锁的线程会排入读线程队列。即使它们能和现有的阅读器锁持有者共享并发访问,也不会给它们的请求授予权限;这有助于防止编写器被阅读器无限期阻止。

大多数在 ReaderWriterLock 上获取锁的方法都采用超时值。使用超时可以避免应用程序中出现死锁。例如,某个线程可能获取了一个资源上的写线程锁,然后请求第二个资源上的读线程锁;同时,另一个线程获取了第二个资源上的写线程锁,并请求第一个资源上的读线程锁。如果不使用超时,这两个线程将出现死锁。

如果超时间隔过期并且没有授予锁请求,则此方法通过引发 ApplicationException 将控制返回给调用线程。线程可以捕捉此异常并确定下一步要进行的操作。

超时用毫秒表示。如果使用 System.TimeSpan 指定超时,则所用的值是 TimeSpan 所表示的毫秒整数的总和。下表显示用毫秒表示的有效超时值。

说明

-1

Infinite.

0

无超时。

> 0

要等待的毫秒数。

除了 -1 以外,不允许使用负的超时值。如果要使用 -1 以外的负整数来指定超时,系统将使用零(无超时)。如果指定的 TimeSpan 表示的是 -1 以外的负毫秒数,将引发 ArgumentOutOfRangeException

示例

' This example shows a ReaderWriterLock protecting a shared
' resource that is read concurrently and written exclusively
' by multiple threads.

' 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

    Const numThreads As Integer = 26
    Private Shared running As Boolean = True
    Private Shared rnd As New Random()
   
    ' Statistics.
    Private Shared readerTimeouts As Integer = 0
    Private Shared writerTimeouts As Integer = 0
    Private Shared reads As Integer = 0
    Private Shared writes As Integer = 0
  
    <MTAThread> _
    Public Shared Sub Main(args() As String)
        ' Start a series of threads. Each thread randomly
        ' performs reads and writes on the shared resource.
        Dim t(numThreads) As Thread
        Dim i As Integer
        For i = 0 To numThreads - 1
            t(i) = New Thread(New ThreadStart(AddressOf ThreadProc))
            t(i).Name = Chr(i + 65)
            t(i).Start()
            If i > 10 Then
                Thread.Sleep(300)
            End If
        Next i 

        ' Tell the threads to shut down, then wait until they all
        ' finish.
        running = False
        For i = 0 To numThreads - 1
            t(i).Join()
        Next i
      
        ' Display statistics.
        Console.WriteLine(vbCrLf & "{0} reads, {1} writes, {2} reader time-outs, {3} writer time-outs.", reads, writes, readerTimeouts, writerTimeouts)
        Console.WriteLine("Press ENTER to exit.")
        Console.ReadLine()
    End Sub 'Main
   
   
    Shared Sub ThreadProc()
        ' As long as a thread runs, it randomly selects
        ' various ways to read and write from the shared 
        ' resource. Each of the methods demonstrates one 
        ' or more features of ReaderWriterLock.
        While running
            Dim action As Double = rnd.NextDouble()
            If action < 0.8 Then
                ReadFromResource(10)
            ElseIf action < 0.81 Then
                ReleaseRestore(50)
            ElseIf action < 0.9 Then
                UpgradeDowngrade(100)
            Else
                WriteToResource(100)
            End If
        End While
    End Sub 'ThreadProc
    
    ' 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

    ' Shows how to request and release the writer lock, and
    ' how to handle time-outs.
    Shared Sub WriteToResource(timeOut As Integer)
        Try
            rwl.AcquireWriterLock(timeOut)
            Try
                ' It is 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 'WriteToResource

    ' Shows how to request a reader lock, upgrade the
    ' reader lock to the writer lock, and downgrade to a
    ' reader lock again.
    Shared Sub UpgradeDowngrade(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)
            
                ' If it is necessary to write to the resource,
                ' you must either release the reader lock and 
                ' then request the writer lock, or upgrade the
                ' reader lock. Note that upgrading the reader lock
                ' puts the thread in the write queue, behind any
                ' other threads that might be waiting for the 
                ' writer lock.
                Try
                    Dim lc As LockCookie = rwl.UpgradeToWriterLock(timeOut)
                    Try
                        ' It is 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.DowngradeFromWriterLock(lc)
                    End Try
                Catch ex As ApplicationException
                    ' The upgrade request timed out.
                    Interlocked.Increment(writerTimeouts)
                End Try
            
                ' When the lock has been downgraded, it is 
                ' still safe to read from the 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 'UpgradeDowngrade

    ' 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

    ' Helper method briefly displays the most recent
    ' thread action. Comment out calls to Display to 
    ' get a better idea of throughput.
    Shared Sub Display(msg As String)
        Console.Write("Thread {0} {1}.       " & vbCr, Thread.CurrentThread.Name, msg)
    End Sub 'Display
End Class 'Test 
// This example shows a ReaderWriterLock protecting a shared
// resource that is read concurrently and written exclusively
// by multiple threads.

// 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;

    const int numThreads = 26;
    static bool running = true;
    static Random rnd = new Random();

    // Statistics.
    static int readerTimeouts = 0;
    static int writerTimeouts = 0;
    static int reads = 0;
    static int writes = 0;

    public static void Main(string[] args)
    {
        // Start a series of threads. Each thread randomly
        // performs reads and writes on the shared resource.
        Thread[] t = new Thread[numThreads];
        for (int i = 0; i < numThreads; i++)
        {
            t[i] = new Thread(new ThreadStart(ThreadProc));
            t[i].Name = new String(Convert.ToChar(i + 65), 1);
            t[i].Start();
            if (i > 10)
                Thread.Sleep(300);
        }

        // Tell the threads to shut down, then wait until they all
        // finish.
        running = false;
        for (int i = 0; i < numThreads; i++)
        {
            t[i].Join();
        }

        // Display statistics.
        Console.WriteLine("\r\n{0} reads, {1} writes, {2} reader time-outs, {3} writer time-outs.",
            reads, writes, readerTimeouts, writerTimeouts);
        Console.WriteLine("Press ENTER to exit.");
        Console.ReadLine();
    }

    static void ThreadProc()
    {
        // As long as a thread runs, it randomly selects
        // various ways to read and write from the shared 
        // resource. Each of the methods demonstrates one 
        // or more features of ReaderWriterLock.
        while (running)
        {
            double action = rnd.NextDouble();
            if (action < .8)
                ReadFromResource(10);
            else if (action < .81)
                ReleaseRestore(50);
            else if (action < .90)
                UpgradeDowngrade(100);
            else
                WriteToResource(100);
        }
    }

    // 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);
        }
    }

    // Shows how to request and release the writer lock, and
    // how to handle time-outs.
    static void WriteToResource(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("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);
        }
    }

    // Shows how to request a reader lock, upgrade the
    // reader lock to the writer lock, and downgrade to a
    // reader lock again.
    static void UpgradeDowngrade(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);

                // If it is necessary to write to the resource,
                // you must either release the reader lock and 
                // then request the writer lock, or upgrade the
                // reader lock. Note that upgrading the reader lock
                // puts the thread in the write queue, behind any
                // other threads that might be waiting for the 
                // writer lock.
                try
                {
                    LockCookie lc = rwl.UpgradeToWriterLock(timeOut);
                    try
                    {
                        // It is safe for this thread to read or write
                        // 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.DowngradeFromWriterLock(ref lc);
                    }
                }
                catch (ApplicationException)
                {
                    // The upgrade request timed out.
                    Interlocked.Increment(ref writerTimeouts);
                }

                // When the lock has been downgraded, it is 
                // still safe to read from the 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);
        }
    }

    // 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);
        }
    }

    // Helper method briefly displays the most recent
    // thread action. Comment out calls to Display to 
    // get a better idea of throughput.
    static void Display(string msg)
    {
        Console.Write("Thread {0} {1}.       \r", Thread.CurrentThread.Name, msg);
    }
}
// This example shows a ReaderWriterLock protecting a shared
// resource that is read concurrently and written exclusively
// by multiple threads.
// 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;

   literal int numThreads = 26;
   static bool running = true;
   static Random^ rnd = gcnew Random;

   // Statistics.
   static int readerTimeouts = 0;
   static int writerTimeouts = 0;
   static int reads = 0;
   static int writes = 0;
   static void ThreadProc()
   {
      
      // As long as a thread runs, it randomly selects
      // various ways to read and write from the shared 
      // resource. Each of the methods demonstrates one 
      // or more features of ReaderWriterLock.
      while ( running )
      {
         double action = rnd->NextDouble();
         if ( action < .8 )
                  ReadFromResource( 10 );
         else
         if ( action < .81 )
                  ReleaseRestore( 50 );
         else
         if ( action < .90 )
                  UpgradeDowngrade( 100 );
         else
                  WriteToResource( 100 );
      }
   }


   // 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 );
      }

   }


   // Shows how to request and release the writer lock, and
   // how to handle time-outs.
   static void WriteToResource( 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 );
      }

   }


   // Shows how to request a reader lock, upgrade the
   // reader lock to the writer lock, and downgrade to a
   // reader lock again.
   static void UpgradeDowngrade( 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 );
            
            // If it is necessary to write to the resource,
            // you must either release the reader lock and 
            // then request the writer lock, or upgrade the
            // reader lock. Note that upgrading the reader lock
            // puts the thread in the write queue, behind any
            // other threads that might be waiting for the 
            // writer lock.
            try
            {
               LockCookie lc = rwl->UpgradeToWriterLock( 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->DowngradeFromWriterLock( lc );
               }

            }
            catch ( ApplicationException^ ) 
            {
               
               // The upgrade request timed out.
               Interlocked::Increment( writerTimeouts );
            }

            
            // When the lock has been downgraded, it is 
            // still safe to read from the 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 );
      }

   }


   // 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( "reads resource value {0}", 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 timeout 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 ) )
            {
               resourceValue = resource;
               Interlocked::Increment( reads );
               Display( String::Format( "resource has changed {0}", resourceValue ) );
            }
            else
            {
               Display( String::Format( "resource has not changed {0}", resourceValue ) );
            }
         }
         finally
         {
            
            // Ensure that the lock is released.
            rwl->ReleaseReaderLock();
         }

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

   }


   // Helper method briefly displays the most recent
   // thread action. Comment out calls to Display to 
   // get a better idea of throughput.
   static void Display( String^ msg )
   {
      Console::Write( "Thread {0} {1}.       \r", Thread::CurrentThread->Name, msg );
   }

};


int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();
   
   // Start a series of threads. Each thread randomly
   // performs reads and writes on the shared resource.
   array<Thread^>^t = gcnew array<Thread^>(Test::numThreads);
   for ( int i = 0; i < Test::numThreads; i++ )
   {
      t[ i ] = gcnew Thread( gcnew ThreadStart( Test::ThreadProc ) );
      t[ i ]->Name = gcnew String( Convert::ToChar( i + 65 ),1 );
      t[ i ]->Start();
      if ( i > 10 )
            Thread::Sleep( 300 );

   }
   
   // Tell the threads to shut down, then wait until they all
   // finish.
   Test::running = false;
   for ( int i = 0; i < Test::numThreads; i++ )
   {
      t[ i ]->Join();

   }
   
   // Display statistics.
   Console::WriteLine( "\r\n {0} reads, {1} writes, {2} reader time-outs, {3} writer time-outs.", Test::reads, Test::writes, Test::readerTimeouts, Test::writerTimeouts );
   Console::WriteLine( "Press ENTER to exit." );
   Console::ReadLine();
   return 0;
}
// This example shows a ReaderWriterLock protecting a shared
// resource that is read concurrently and written exclusively
// by multiple threads.

// The complete code is located in the ReaderWriterLock
// class topic.
import System.*;
import System.Threading.*;
import System.Threading.Thread;    

public class Test
{
    // Declaring the ReaderWriterLock at the class level
    // makes it visible to all threads.
    private static ReaderWriterLock rwl = new ReaderWriterLock();

    // For this example, the shared resource protected by the
    // ReaderWriterLock is just an integer.
    private static int resource = 0;
    private static int numThreads = 26;
    private static boolean running = true;
    private static Random rnd = new Random();

    // Statistics.
    private static int readerTimeouts = 0;
    private static int writerTimeouts = 0;
    private static int reads = 0;
    private static int writes = 0;

    public static void main(String[] args)
    {
        // Start a series of threads. Each thread randomly
        // performs reads and writes on the shared resource.
        Thread t[] = new Thread[numThreads];

        for (int i = 0; i < numThreads; i++) {
            t[i] = new Thread(new ThreadStart(ThreadProc));
            t[i].set_Name(new String(Convert.ToChar((i + 65)), 1));
            t[i].Start();
            if (i > 10) {
                Thread.Sleep(300);
        }
    } //main

    // Tell the threads to shut down, then wait until they all
    // finish.
    running = false;
    for (int i = 0; i < numThreads; i++) {
        t[i].Join();
    }

    // Display statistics.
    Console.WriteLine("\r\n{0} reads, {1} writes, {2} reader time-outs," 
        + " {3} writer time-outs.",
        new Object[] { (Int32)(reads), (Int32)(writes), 
        (Int32)(readerTimeouts), (Int32)(writerTimeouts) });
    Console.WriteLine("Press ENTER to exit.");
    Console.ReadLine();
} //Test


    static void ThreadProc()
    {
        // As long as a thread runs, it randomly selects
        // various ways to read and write from the shared 
        // resource. Each of the methods demonstrates one 
        // or more features of ReaderWriterLock.
        while (running) {
            double action = rnd.NextDouble();

            if (action < 0.8) {
                ReadFromResource(10);
            }
            else {
                if (action < 0.81) {
                    ReleaseRestore(50);
                }
                else {
                    if (action < 0.9) {
                        UpgradeDowngrade(100);
                    }
                    else {
                        WriteToResource(100);
                    }
                }
            }
        }
    } //ThreadProc

    // 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(reads);
            }
            finally {
                // Ensure that the lock is released.
                rwl.ReleaseReaderLock();
            }
        }
        catch (ApplicationException exp) {
            // The reader lock request timed out.
            Interlocked.Increment(readerTimeouts);
        }
    } //ReadFromResource

    // Shows how to request and release the writer lock, and
    // how to handle time-outs.
    static void WriteToResource(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(("writes resource value " + resource));
                Interlocked.Increment(writes);
            }
            finally {
                // Ensure that the lock is released.
                rwl.ReleaseWriterLock();
            }
        }
        catch (ApplicationException exp) {
            // The writer lock request timed out.
            Interlocked.Increment(writerTimeouts);
        }
    } //WriteToResource

    // Shows how to request a reader lock, upgrade the
    // reader lock to the writer lock, and downgrade to a
    // reader lock again.
    static void UpgradeDowngrade(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(reads);

                // If it is necessary to write to the resource,
                // you must either release the reader lock and 
                // then request the writer lock, or upgrade the
                // reader lock. Note that upgrading the reader lock
                // puts the thread in the write queue, behind any
                // other threads that might be waiting for the 
                // writer lock.
                try {
                    LockCookie lc = rwl.UpgradeToWriterLock(timeOut);

                    try {
                        // It is 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.DowngradeFromWriterLock(lc);
                    }
                }
                catch (ApplicationException exp) {
                    // The upgrade request timed out.
                    Interlocked.Increment(writerTimeouts);
                }

                // When the lock has been downgraded, it is 
                // still safe to read from the resource.
                Display(("reads resource value " + resource));
                Interlocked.Increment(reads);
            }
            finally {
                // Ensure that the lock is released.
                rwl.ReleaseReaderLock();
            }
        }
        catch (ApplicationException exp) {
            // The reader lock request timed out.
            Interlocked.Increment(readerTimeouts);
        }
    } //UpgradeDowngrade


    // 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(reads);

                // Save the current writer sequence number.
                lastWriter = rwl.get_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(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(("resource has changed " + resourceValue));
                }
                else {
                    Display(("resource has not changed " + resourceValue));
                }
            }
            finally {
                // Ensure that the lock is released.
                rwl.ReleaseReaderLock();
            }
        }
        catch (ApplicationException exp) {
            // The reader lock request timed out.
            Interlocked.Increment(readerTimeouts);
        }
    } //ReleaseRestore

    // Helper method briefly displays the most recent
    // thread action. Comment out calls to Display to 
    // get a better idea of throughput.
    static void Display(String msg)
    {
        Console.Write("Thread {0} {1}.       \r",
            Thread.get_CurrentThread().get_Name(), msg);
    } //Display
}

继承层次结构

System.Object
   System.Runtime.ConstrainedExecution.CriticalFinalizerObject
    System.Threading.ReaderWriterLock

线程安全

该类型对于多线程操作是安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ReaderWriterLock 成员
System.Threading 命名空间

其他资源

托管线程处理
读取器/编写器锁