本隨筆續接:.NET 同步與異步 之 Mutex (十二)html
在前一篇咱們已經提到過Mutex和本篇的主角們直接或間接繼承自 WaitHandle:數組
WaitHandle提供了若干用於同步的方法。上一篇關於Mutex的blog中已經講到一個WaitOne(),這是一個實例方法。除此以外,WaitHandle另有3個用於同步的靜態方法:安全
線程相關性異步
Event通知ide
EventWaitHandle、AutoResetEvent、ManualResetEvent名字裏都有一個「Event」,不過這跟.net的自己的事件機制徹底沒有關係,它不涉及任何委託或事件處理程序。相對於咱們以前碰到的Monitor和Mutex須要線程去爭奪「鎖」而言,咱們能夠把它們理解爲一些須要線程等待的「事件」。線程經過等待這些事件的「發生」,把本身阻塞起來。一旦「事件」完成,被阻塞的線程在收到信號後就能夠繼續工做。函數
爲了配合WaitHandle上的3個靜態方法SingnalAndWait()/WailAny()/WaitAll(),EventWaitHandle提供了本身獨有的,使「Event」完成和從新開始的方法:oop
構造函數post
來看看EventWaitHandle衆多構造函數中最簡單的一個:spa
好了,如今咱們能夠清楚的知道Set()在何時分別相似於Monitor.Pulse()/PulseAll()了:.net
來看看EventWaitHandle的其它構造函數:
MSDN Demo
using System; using System.Threading; public class Example { // The EventWaitHandle used to demonstrate the difference // between AutoReset and ManualReset synchronization events. // private static EventWaitHandle ewh; // A counter to make sure all threads are started and // blocked before any are released. A Long is used to show // the use of the 64-bit Interlocked methods. // private static long threadCount = 0; // An AutoReset event that allows the main thread to block // until an exiting thread has decremented the count. // private static EventWaitHandle clearCount = new EventWaitHandle(false, EventResetMode.AutoReset); [MTAThread] public static void Main() { // Create an AutoReset EventWaitHandle. // ewh = new EventWaitHandle(false, EventResetMode.AutoReset); // Create and start five numbered threads. Use the // ParameterizedThreadStart delegate, so the thread // number can be passed as an argument to the Start // method. for (int i = 0; i <= 4; i++) { Thread t = new Thread( new ParameterizedThreadStart(ThreadProc) ); t.Start(i); } // Wait until all the threads have started and blocked. // When multiple threads use a 64-bit value on a 32-bit // system, you must access the value through the // Interlocked class to guarantee thread safety. // while (Interlocked.Read(ref threadCount) < 5) { Thread.Sleep(500); } // Release one thread each time the user presses ENTER, // until all threads have been released. // while (Interlocked.Read(ref threadCount) > 0) { Console.WriteLine("Press ENTER to release a waiting thread."); Console.ReadLine(); // SignalAndWait signals the EventWaitHandle, which // releases exactly one thread before resetting, // because it was created with AutoReset mode. // SignalAndWait then blocks on clearCount, to // allow the signaled thread to decrement the count // before looping again. // WaitHandle.SignalAndWait(ewh, clearCount); } Console.WriteLine(); // Create a ManualReset EventWaitHandle. // ewh = new EventWaitHandle(false, EventResetMode.ManualReset); // Create and start five more numbered threads. // for(int i=0; i<=4; i++) { Thread t = new Thread( new ParameterizedThreadStart(ThreadProc) ); t.Start(i); } // Wait until all the threads have started and blocked. // while (Interlocked.Read(ref threadCount) < 5) { Thread.Sleep(500); } // Because the EventWaitHandle was created with // ManualReset mode, signaling it releases all the // waiting threads. // Console.WriteLine("Press ENTER to release the waiting threads."); Console.ReadLine(); ewh.Set(); } public static void ThreadProc(object data) { int index = (int) data; Console.WriteLine("Thread {0} blocks.", data); // Increment the count of blocked threads. Interlocked.Increment(ref threadCount); // Wait on the EventWaitHandle. ewh.WaitOne(); Console.WriteLine("Thread {0} exits.", data); // Decrement the count of blocked threads. Interlocked.Decrement(ref threadCount); // After signaling ewh, the main thread blocks on // clearCount until the signaled thread has // decremented the count. Signal it now. // clearCount.Set(); } }
附,Demo : http://files.cnblogs.com/files/08shiyan/ParallelDemo.zip
參見更多:隨筆導讀:同步與異步
(該系列隨筆暫告一段落)