当前位置:首页>网络学院>程序开发>c#教程>文章内容

c#线程同步中的lock,monitor,同步事件和等待句柄以及mutex的介绍

[ 来源:http://www.it55.com | 作者: | 时间:2007-12-13 | 收藏 | 推荐 ] 【


   static void ThreadPongProc() {
   System.Console.WriteLine("ThreadPong: Hello!");
   lock ( ball )
   for (int i = 0; i < 5; i++){
   System.Console.WriteLine("ThreadPong: Pong ");
   Monitor.Pulse( ball );
   Monitor.Wait( ball );
   }
   System.Console.WriteLine("ThreadPong: Bye!");
   }
   static void ThreadPingProc() {
   System.Console.WriteLine("ThreadPing: Hello!");
   lock ( ball )
   for(int i=0; i< 5; i++){
   System.Console.WriteLine("ThreadPing: Ping ");
   Monitor.Pulse( ball );
   Monitor.Wait( ball );
   }
   System.Console.WriteLine("ThreadPing: Bye!");
   }
  }
   执行结果如下(有可能是ThreadPong先执行): ThreadPing: Hello!
  ThreadPing: Ping
  ThreadPong: Hello!
  ThreadPong: Pong
  ThreadPing: Ping
  ThreadPong: Pong
  ThreadPing: Ping
  ThreadPong: Pong
  ThreadPing: Ping
  ThreadPong: Pong
  ThreadPing: Ping
  ThreadPong: Pong
  ThreadPing: Bye!
   当threadPing进程进入ThreadPingProc锁定ball并调用Monitor.Pulse( ball );后,它通知threadPong从阻塞队列进入准备队列,当threadPing调用Monitor.Wait( ball )阻塞自己后,它放弃了了对ball的锁定,所以threadPong得以执行。PulseAll与Pulse方法类似,不过它是向所有在阻塞队列中的进程发送通知信号,如果只有一个线程被阻塞,那么请使用Pulse方法。
  同步事件和等待句柄
   同步事件和等待句柄用于解决更复杂的同步情况,比如一个一个大的计算步骤包含3个步骤result = first term + second term + third term,如果现在想写个多线程程序,同时计算first term,second term 和third term,等所有3个步骤计算好后再把它们汇总起来,我们就需要使用到同步事件和等待句柄,同步事件分有两个,分别为AutoResetEvent和ManualResetEvent,这两个类可以用来代表某个线程的运行状态:终止和非终止,等待句柄用来判断ResetEvent的状态,如果是非终止状态就一直等待,否则放行,让等待句柄下面的代码继续运行。下面的代码示例阐释了如何使用等待句柄来发送复杂数字计算的不同阶段的完成信号。此计算的格式为:result = first term + second term + third term using System;
  using System.Threading;
  class CalculateTest
  {
   static void Main()
   {
   Calculate calc = new Calculate();
   Console.WriteLine("Result = {0}.",
   calc.Result(234).ToString());
   Console.WriteLine("Result = {0}.",
   calc.Result(55).ToString());
   }
  }
  class Calculate
  {
   double baseNumber, firstTerm, secondTerm, thirdTerm;
   AutoResetEvent[] autoEvents;
   ManualResetEvent manualEvent;
   // Generate random numbers to simulate the actual calculations.
   Random randomGenerator;
   public Calculate()
   {
   autoEvents = new AutoResetEvent[]
   {
   new AutoResetEvent(false),
   new AutoResetEvent(false),
   new AutoResetEvent(false)
   };
   manualEvent = new ManualResetEvent(false);
   }
   void CalculateBase(object stateInfo)
   {
   baseNumber = randomGenerator.NextDouble();
  
   // Signal that baseNumber is ready.
   manualEvent.Set();
   }
   // The following CalculateX methods all perform the same
   // series of steps as commented in CalculateFirstTerm.
   void CalculateFirstTerm(object stateInfo)
   {
   // Perform a precalculation.
   double preCalc = randomGenerator.NextDouble();
   // Wait for baseNumber to be calculated.
   manualEvent.WaitOne();
   // Calculate the first term from preCalc and baseNumber.
   firstTerm = preCalc * baseNumber *
   randomGenerator.NextDouble();
   // Signal that the calculation is finished.
   autoEvents[0].Set();
   }
   void CalculateSecondTerm(object stateInfo)
   {
   double preCalc = randomGenerator.NextDouble();
   manualEvent.WaitOne();
   secondTerm = preCalc * baseNumber *
   randomGenerator.NextDouble();
   autoEvents[1].Set();
   }
   void CalculateThirdTerm(object stateInfo)

(编辑:IT资讯之家 www.it55.com

返回顶部
共3页: 上一页 [1] 2 [3] 下一页  

网友评论

[以下评论为网友观点,不代表本站。请自觉遵守互联网相关政策法规,所有连带责任均有评论者自负。]
[不超过250字]