C uses AutoResetEvent for synchronization

  • 2021-12-12 05:19:28
  • OfStack

A few days ago, I encountered the problem of sequential execution of one thread, that is, one asynchronous thread sent one data request to A interface. Another asynchronous thread sends a data request to the B interface, and when both A and B are executed successfully, it sends a request to the C interface. To be honest, I don't know much about threads when I do BS project. I know that AutoResetEvent is related to threads and is used to deal with thread switching, so I decided to use AutoResetEvent to deal with the above problems.

So find relevant information online:

Originally, AutoResetEvent is often used in. Net multithreaded programming. When a thread calls the WaitOne method, the signal is sent, the thread gets the signal, and the program continues to execute down, otherwise it waits. Moreover, AutoResetEvent. WaitOne () only allows one thread to enter at a time. When a certain thread gets a signal, AutoResetEvent will automatically set the signal to a non-sending state, and other threads calling WaitOne will only continue to wait. That is to say, AutoResetEvent1 only wakes up one thread at a time, and other threads are still blocked.

Brief introduction

AutoResetEvent (bool initialState): Constructor that initializes a new instance of the class with a Boolean value indicating whether to set the initial state to terminated.
false: No signal, the child thread's WaitOne method will not be automatically called
true: When signaled, the child thread's WaitOne method is automatically called
Reset (): Sets the event state to a non-terminated state, causing the thread to block; If the operation is successful, true; is returned; Otherwise, false is returned.
Set (): Sets the event state to terminated state, allowing one or more waiting threads to continue; If the operation is successful, true; is returned; Otherwise, false is returned.
WaitOne (): Blocks the current thread until a signal is received.
WaitOne (TimeSpan, Boolean): Blocks the current thread until the current instance receives a signal, using TimeSpan to measure the time interval and specify whether to exit the synchronization domain before waiting.
WaitAll (): Wait for all signals.

Realization


 class Program
 {

  static void Main()
  {
   Request req = new Request();

   // This man will do it 3 A great event  
   Thread GetCarThread = new Thread(new ThreadStart(req.InterfaceA));
   GetCarThread.Start();

   Thread GetHouseThead = new Thread(new ThreadStart(req.InterfaceB));
   GetHouseThead.Start();

   // Wait 3 Good news notification message that everything has been done  
   AutoResetEvent.WaitAll(req.autoEvents);

   // This person will be happy.  
   req.InterfaceC();

   System.Console.ReadKey();
  }
 }

 public class Request
 {
  // Creating an Event Array  
  public AutoResetEvent[] autoEvents = null;

  public Request()
  {
   autoEvents = new AutoResetEvent[]
   {
    new AutoResetEvent(false),
    new AutoResetEvent(false)
   };
  }

  public void InterfaceA()
  {
   System.Console.WriteLine(" Request A Interface ");

   Thread.Sleep(1000*2);

   autoEvents[0].Set();

   System.Console.WriteLine("A Interface completion ");
  }

  public void InterfaceB()
  {
   System.Console.WriteLine(" Request B Interface ");

   Thread.Sleep(1000 * 1);

   autoEvents[1].Set();

   System.Console.WriteLine("B Interface completion ");
  }

  public void InterfaceC()
  {
   System.Console.WriteLine(" Both interfaces have been requested and are being processed C");
  }
 }

Note that it is best to add a timeout to WaitOne or WaitAll. Otherwise, no signal is received, and thread 1 will block.

Afterword

This is just a simplification of the above scenario, which is mainly used to solve the problem of the scenario I just mentioned.
The above is my own summary of the use of AutoResetEvent. Please point out the shortcomings 12.


Related articles: