Multi-thread AutoResetEvent

Source: Internet
Author: User

When we program the thread, we tend to involve the communication of the threads and the operation of whether the thread is blocked by the reception of the signal.

AutoResetEvent allows threads to communicate with each other by signaling. Typically, this communication involves a resource that the thread requires exclusive access to.

There are many methods of AutoResetEvent , specific methods and extension methods are described in the AutoResetEvent class, the most commonly used methods are set () and WaitOne ().

the thread waits for a signal by calling WaitOne on AutoResetEvent . If the AutoResetEvent is in a non-terminating state, the thread blocks and waits for the current control resource's thread to emit a signal that the resource is available by calling Set. The non-terminating state of the AutoResetEvent can be set through the constructor :

Static New AutoResetEvent (false);

The argument false in the constructor here means that the state is non-terminating, and the opposite is the terminating state if true.

In layman's terms, only after the successful operation of the Myreseteven.set (), Myreseteven.waitone () will be able to obtain operational opportunities; Set is the signal, WaitOne is waiting for the signal, only sent a signal,
Wait for it to execute. If not, the program behind WaitOne will never be executed. Let's give an example:

 Public classProgram {StaticAutoResetEvent myresetevent =NewAutoResetEvent (false); Const intCyclenum =5; Static voidMain (string[] args) {Thread TD=NewThread (NewThreadStart (sqrt)); Td. Name="Thread One"; Td.            Start ();        Console.readkey (); }        /// <summary>        ///Calculate squared/// </summary>        /// <param name= "i" ></param>         Public Static voidsqrt () {myresetevent.waitone (); Console.WriteLine (DateTime.Now.ToShortTimeString ()+"Thread One execution"); Thread.Sleep ( -); }    }

The above example starts with a non-terminating state, which blocks the thread when the WaitOne () method is encountered, and will remain blocked when no set () runs as follows:

Next we execute the set () method in the main function to liberate the blocked thread:

 Public classProgram {StaticAutoResetEvent myresetevent =NewAutoResetEvent (false); Const intCyclenum =5; Static voidMain (string[] args) {Thread TD=NewThread (NewThreadStart (sqrt)); Td. Name="Thread One"; Td.            Start (); Myresetevent.set ();//the WaitOne method is blocked, and the Set () method continues after executionConsole.readkey (); }        /// <summary>        ///Calculate squared/// </summary>        /// <param name= "i" ></param>         Public Static voidsqrt () {myresetevent.waitone (); Console.WriteLine (DateTime.Now.ToShortTimeString ()+"Thread One execution"); Thread.Sleep ( -); }    }

The results of the operation are as follows:

Since speaking of AutoResetEvent, we have to say ManualResetEvent, these two methods are almost the same, the difference lies in the AutoResetEvent WaitOne ( When the method is executed, the signal is automatically set to the non-sending State, that is, the blocking state, when the WaitOne () method is again blocked, and AutoResetEvent does not, as long as the thread is in a non-blocking state regardless of the number of times it encounters WaitOne () method is not blocked unless the reset () method is called to manually block the thread. Here will not run the results, interested friends can try their own.

This blog does not guarantee that all the information is correct, there are errors also want to point out.

According to the seal Sword White
Source: Learning: Multi-thread AutoResetEvent
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. If you have any questions or suggestions, please enlighten me, thank you very much.

Multi-thread AutoResetEvent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.