1024programmer Asp.Net [.NET] Multithreading: The difference between automatic reset events and manual reset events

[.NET] Multithreading: The difference between automatic reset events and manual reset events

【.NET】Multi-threading: The difference between automatic reset events and manual reset events

In multi-threaded programming, if each thread does not run completely independently. Then, a thread needs to know what happened to other threads at a certain point in its execution. Well, this is called thread synchronization. The synchronization event object (XXXEvent) has two behaviors:

1. Wait. The thread will pause at this time and wait for other threads to send signals before continuing (waiting for your appointment);

2. Send a signal. The current thread sends a signal, and other waiting threads continue to run after receiving the signal (I will ask you).

Once upon a time, Xiao Ming, Xiao Wei, Xiao Geng, Xiao Hong, and Xiao Huang planned to go to the wild to grill fish. However, they only identified an area in the southeastern suburbs of the city and could not guarantee which location would be suitable for barbecue. So, they agreed that everyone would start from home at the same time. Xiao Ming was relatively close there, so he went to investigate first; the others gathered after arriving at the southeastern suburbs, waiting for news about Xiao Ming. After Xiao Ming completed the inspection, he sent a group message to everyone stating that the selected location was F. Finally, everyone moved on and headed towards F.

There are several waiting events:

1. Mutex: Mutex. Only one thread can obtain the mutex at a time, and other threads can only wait. After the thread occupying the mutex is released, other threads continue to grab the Mutex. Then only one thread can grab it, and other threads continue to wait…

2. AutoResetEvent: automatic event, reset immediately after sending a signal.

3. ManualResetEvent: Manual event. It will not be reset immediately after sending the signal. It must be reset manually.

4. CountdownEvent: This is similar to the above two. But it sets a count that is decremented when the thread emits a signal. The blocked thread does not get the signal until count <= 0.

The focus of our discussion this time is to look at the automatic reset signal and manual reset What is the difference between signals.

Let’s take a look at the automatic reset first.

internal class Program

     static AutoResetEvent   theEvent = new(false);

     static void  span> Main(string[] args  )
         // Start three  Threads
         ThreadPool.QueueUserWorkItem(DoWorking, "A  ");
         ThreadPool.QueueUserWorkItem(DoWorking, "B");
         ThreadPool.QueueUserWorkItem(DoWorking, "C");
         // Main thread  Monitor keyboard messages
             var keyInfo = Console.ReadKey(true);
             // Take a look  Is it the Y key?
             if(keyInfo.Key == ConsoleKey  .Y)
                 // light up  signal
             // Output one  �8, 0, 0, 1)">");
         ThreadPool.QueueUserWorkItem(DoSomething, "C");

             // Read  Keyboard characters
             ConsoleKeyInfo info = Console.ReadKey(true  );
             //  Put in queue

     static void  span> DoSomething(object? state)
             // Lock
             if (  keyChars.Count > 0)
                 // Remove  an element
                 char c = keyChars.Dequeue()  ;
                 Console.WriteLine($"Thread [{state}] obtains characters: {c}");
             // Unlock

Here I use a generic queue Queue to store the characters typed by the keyboard, and the DoSomething method will be put into the thread pool to run. When removing elements from the queue and processing them, be sure to lock them. I use the static methods of the Monitor object to lock and unlock, but of course you can use the lock statement block.


If the lock is not locked, inconsistent states will occur when threads seize resources. When thread A accesses the keyChars.Count property, it gets 1, which is still > 0, but before taking out the last element, thread B happens to take the last element away. When thread A executes the keyChars.Dequeue() sentence, there are no elements in the keyChars queue, and an error will occur.

The main thread does not need to lock when enqueueing, because only one thread is doing the sending of elements into the queue, and no one is competing with it to grab resources, so it does not need to be locked.

After running the program, you can press letters, numbers and other keys to test. After all, keys such as [F3] and [Ctrl] obtain blank char.

This will be much smoother.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/811345

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top