Synchronize two threads Benpian EventWaitHandle by using a Liezi. Please see the following Liezi.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ThreadSynDemo { class Program { private int n1, n2, n3; static void Main(string[] args) { Program p = new Program(); Thread t0 = new Thread(new ThreadStart(p.WriteThread)); Thread t1=newThe Thread ( new new the ThreadStart (p.ReadThread)); t0.Start (); t1.Start (); Console.ReadLine (); } Private void WriteThread () { Console.WriteLine ( " WriteThread " ); N1 = . 1 ; N2 = 2 ; N3 = 3 ; } // sometimes result 0 + 0 + 0 = 0; sometimes the result is 1 + 2 + 3 = 6; sequential execution because the thread leading to uncertainty. // we need the result is always 1 + 2 + 3 = 6; the need to ensure WriteThread () than ReadThread () early execution. This would involve synchronization of threads. private void ReadThread() { Console.WriteLine("{0}+{1}+{2}={3}",n1,n2,n3,n1+n2+n3); } } }
Liezi above may result 0 + 0 + 0 = 0; sometimes the result is 1 + 2 + 3 = 6; sequential execution because the thread leading to uncertainty. We need the result is always 1 + 2 + 3 = 6; the need to ensure WriteThread () than ReadThread () early execution. This would involve synchronization of threads.
Here we synchronize these two threads with EventWaitHandle. Consider the following Liezi:
a using System; a using System.Collections.Generic; a using System.Linq; a using System.Text; a using the System.Threading; // Here we use EventWaitHandle class synchronize threads // EventWaitHandle categories: operating system allows signals emitted by the completion of synchronization between threads need to synchronize multiple threads can block the current thread. // then the Windows operating system based on a signal issued, the decision block waiting for completion ,, or other directly proceed without waiting // the EventWaitHandle class as follows: // (. 1) of the Reset state signal into a non-terminating state ( do not let the operating system sends signals), resulting in those threads only receive signals to continue in the blocked state // (2) the set: the event status to terminate the state, so the waiting thread will receive signal, thus waiting Switch to proceed // (3) WaitOne method: blocking the current thread, waiting for the operating system for its signals until it receives a signal only unblocked // operating system sends out signals of two ways: //(1) send a signal to a thread waiting for the signal unblocked, continue with automatic reset (AutoResetEvent) // (2) send a signal to all the threads waiting for the signal of all the unblocked continue. Manual reset set (ManualRestEvent) namespace ThreadAsyDemo2 { class Program { Private int N1, N2, N3; // to set the non-signal state terminated state, using manually reset the EventWaitHandle myEventWaitHandle = new new the EventWaitHandle ( to false , EventResetMode.ManualReset); static void the Main ( String [] args) { Program P = new new Program (); the Thread T0 = new new the Thread (new new the ThreadStart (p.WriteThread)); the Thread T1 = new new the Thread ( new new the ThreadStart (p.ReadThread)); t0.Start (); t1.Start (); Console.ReadLine (); } Private void WriteThread () { / / allow other threads to wait blocked myEventWaitHandle.Reset (); Console.WriteLine ( " WriteThread " ); N1 = . 1 ; N2 = 2 ; N3 = . 3; // allow other threads to continue waiting myEventWaitHandle.Set (); } // sometimes result 0 + 0 + 0 = 0; sometimes the result is 1 + 2 + 3 = 6; sequential execution because the thread leading to uncertainty. // we need the result is always 1 + 2 + 3 = 6; the need to ensure WriteThread () than ReadThread () early execution. This would involve synchronization of threads. Private void ReadThread () { // blocks the current thread until the received signal myEventWaitHandle.WaitOne (); Console.WriteLine ( " {0} + {. 1} {2} = {+}. 3 " , N1, N2, N3, + N2 + N1 N3); } } }
Reprinted to: https://www.cnblogs.com/Joetao/articles/3152497.html