Objective
When we write a multithreaded program, we encounter the problem of having to wait for the result of another thread's processing to continue to execute while one thread processes. For example: There are two threads, one to receive the socket data, the other to process the socket data, and the thread that handles the socket data needs to be processed after receiving the socket data, waiting for the receiving thread to receive the data. So how does the processing thread wait, and how does the receiving thread notify the processing thread?
One of the better ways is to use autoresetevent/manualresetevent
1. Autoresetevent/manualresetevent Introduction
Autoresetevent/manualresetevent is the object that. NET provides to us for inter-thread synchronization. There are three main functions:
WaitOne: For blocking threads, waiting to receive a signal to continue running
Set: Used to send a sync signal to notify the waiting thread to continue running
Reset: Reset Termination status
Initializes the constructor resetevent (bool initialstate), which, when Initialstate is true, is signaled by default (that is, blocking is not valid), and when Initialstate is false, the default is the non-terminating state
As to the termination and non-termination of the resetevent, it will be difficult to understand when people start to see it, and I will give you a very vivid example.
We all have to work card into the building experience, here can think of ResetEvent as the gate, only swipe to enter.
WaitOne is waiting for the card, set is the swipe, and reset is the closed gate, so that when the initialization, initialstate is true, meaning that the default gate is open; Initialstate is false, the default gate is off
So, if you want to enter the building, the steps should be like this (assuming the current gate is closed),
Wait Card (WaitOne), swipe (Set)----shut-off gate (Reset)
2. The difference between autoresetevent/manualresetevent
In fact, the literal meaning of these two classes has told us the difference between them, since reset is the closed gate, that is to say, auto is auto-closing the gate, manual is the manual shut-off gate.
AutoResetEvent: After swipe through, the gate automatically shuts down and then waits for the next swipe
ManualResetEvent: After the card pass, the gate will not automatically shut down, if not manually shut down (call the Reset method), waiting for the card is not valid, people will not have to swipe to directly through
So whether the WaitOne is valid, blocking the thread depends on whether the reset method is called.
When Initialstate is true in the constructor, it means that the gate is opened by default for the first time
3. Autoresetevent/manualresetevent Use Example
Next, let's look at the actual effect of these two classes with a simple example.
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;namespace Reseteventtest{class Program {
The default is the non-terminating state at initialization false static AutoResetEvent Autore = new AutoResetEvent (false); static ManualResetEvent Manualre = new ManualResetEvent (false); static void Main (string[] args) {(New Thread (AUTOMETHOD1)). Start (); (New Thread (AUTOMETHOD2)). Start (); (New Thread (MANUALMETHOD1)). Start (); (New Thread (MANUALMETHOD2)). Start (); Console.readkey (); } static void AutoMethod1 () {Console.WriteLine ("Wait print Automethod 1:"); Autore.waitone (); Console.WriteLine ("Automethod 1"); } static void AutoMethod2 () {Console.WriteLine ("Wait Print Automethod 2:"); Autore.waitone (); Console.WriteLine ("Automethod 2"); } static void ManualMethod1 () {Console.WriteLine ("Wait print Manualmethod 1:"); Manualre.waitone (); Console.WriteLine ("Manualmethod 1"); } static void ManualMethod2 () {Console.WriteLine ("Wait Print Manualmethod 2:"); Manualre.waitone (); Console.WriteLine ("Manualmethod 2"); } }}
The corresponding AutoResetEvent and ManualResetEvent to start two threads, all into the wait, this time the output is:
We found that the thread was all blocked, which means false at initialization, which is set to block state, then we change the initialization parameter to true to try
static AutoResetEvent Autore = new AutoResetEvent (true); static ManualResetEvent Manualre = new ManualResetEvent (true);
The results are as follows:
Found (note that the output order may be different), auto only blocked one, and manual two are not blocked, all output, in fact, auto release one, immediately after the reset method is automatically called, resulting in AutoMethod2 can not continue to run without getting the set traffic signal and manual because there is no manual call to the Reset method, WaitOne has no effect on the dummy.
Also use the previous code to add the set code when starting the thread, as follows:
static AutoResetEvent Autore = new AutoResetEvent (false), static ManualResetEvent Manualre = new ManualResetEvent (false); static void Main (string[] args) { (new Thread (AUTOMETHOD1)). Start (); Autore.set (); (New Thread (AUTOMETHOD2)). Start (); (New Thread (MANUALMETHOD1)). Start (); Manualre.set (); (New Thread (MANUALMETHOD2)). Start (); Console.readkey ();}
When the AUTOMETHOD1 thread is started and the set method is called, the AUTOMETHOD1 thread gets a signal to continue running, starting the AUTOMETHOD2 thread because the AUTOMETHOD1 thread is automatically reset after it is run. So the AUTOMETHOD2 will block, and when the MANUALMETHOD1 thread is called set, because the reset state is not manually called, then the MANUALMETHOD2 will not block, and the following results verify our idea:
?
If you want to ManualMethod2 blocking, then once you get the set signal in MANUALMETHOD2, you can call the Reset method
static void ManualMethod1 () { Console.WriteLine ("Wait print Manualmethod 1:"); Manualre.waitone (); Manualre.reset (); Console.WriteLine ("Manualmethod 1");}
Results:
4. Other operations
In fact, we look at the inheritance of ResetEvent to find out that ResetEvent inherits from SignalAndWait under the abstract class Waithandle,waithandle, The WaitAll and WaitAny methods provide a more diverse way of controlling thread synchronization, which will be explored further in the future.
Service summary-Multithreading-thread synchronization (AutoResetEvent and ManualResetEvent)