Reprint Address: http://www.ithao123.cn/content-4805192.html
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;namespace Multithreaded Pool Experiment {class Program {public static void Main () {//New ManualResetEvent object and initialized to no signal state ManualResetEvent eventx = new ManualResetEvent (false); Threadpool.setmaxthreads (3, 3); THR T = new thr (EVENTX); for (int i = 0; i < i++) {ThreadPool.QueueUserWorkItem (new WaitCallback (T.threadproc), I ); }//waits for the event to complete, that is, the thread calls the Manualresetevent.set () method//eventx.waitone blocks the current thread until the current WaitHandle receives a signal. Eventx.waitone (Timeout.infinite, true); Console.WriteLine ("Breakpoint Test"); Thread.Sleep (10000); Console.WriteLine ("Run End"); } public class Thr {public thr (int count,manualresetevent mre) {IMax Count = Count; Eventx = MRE; } public static int iCount = 0; public static int imaxcount = 0; public ManualResetEvent Eventx; public void ThreadProc (Object i) {Console.WriteLine ("thread[" + i.tostring () + "]"); Thread.Sleep (2000); Interlocked.Increment () operation is an atomic operation, the function is: icount++ specific See the following description//atomic operation, is not a higher level interrupt Rob priority operation. If you ask this question, I'll speak a little deeper. Because the operating system is in an interrupt state for most of the time,//So a program may be interrupted by a higher priority thread when it executes. And some operations can not be interrupted, or there will be an inability to restore the consequences, at this time, these operations require atomic operation. is an operation that cannot be interrupted. Interlocked.Increment (ref ICount); if (ICount = = Imaxcount) {Console.WriteLine ("Send End signal!"); Sets the event state to the signaled state, allowing one or more waiting threads to continue. Eventx.set (); } } } }}
Reprint Address: http://www.ithao123.cn/content-4805192.html
Thread pool set up m threads to perform n tasks