Use of AutoResetEvent

Source: Internet
Author: User

AutoResetEventAllows threads to communicate with each other by sending signals. Generally, this communication involves resources that the thread needs to exclusively access.

The thread callsAutoResetEventTo wait for the signal. IfAutoResetEventIf the thread is not terminated, the thread is blocked and waits for the thread to control the resource.
Call Set to send a signal that resources are available.

CallSetDirectionAutoResetEventSend a signal to release the waiting thread.AutoResetEventWill remain terminated until a waiting thread is released, and then automatically return non-terminated
Status. If no thread is waiting, the state is permanently terminated.

You can control it by passing a Boolean value to the constructor.AutoResetEventIf the initial status is terminatedTrueOtherwiseFalse.

In general, only after myResetEven. Set () runs successfully will myResetEven. WaitOne () be able to get the running opportunity; Set is a signal, WaitOne is a waiting signal, and only a signal is sent,
Wait for execution. If it is not sent, the program after WaitOne will never be executed. Let's take an example: I went to the bookstore to buy books. When I selected a book, I would pay for it at the toll booth,
Pay the money and go to the warehouse to get the book. This order cannot be reversed. I act as the main thread, and there are two auxiliary threads at the toll booth and the warehouse. The Code is as follows:

Using System; using System. linq; using System. activities; using System. activities. statements; using System. threading; namespace CaryAREDemo {class Me {const int numIterations = 550; static AutoResetEvent myResetEvent = new AutoResetEvent (false); static AutoResetEvent ChangeEvent = new AutoResetEvent (false ); // static ManualResetEvent myResetEvent = new ManualResetEvent (false); // static ManualResetEvent ChangeEvent = new ManualResetEvent (false); static int number; // This is the key resource static void Main () {Thread payMoneyThread = new Thread (new ThreadStart (PayMoneyProc); payMoneyThread. name = "Paying Thread"; Thread getBookThread = new Thread (new ThreadStart (GetBookProc); getBookThread. name = "Book Receiving thread"; payMoneyThread. start (); getBookThread. start (); for (int I = 1; I <= numIterations; I ++) {Console. writeLine ("book buying thread: quantity {0}", I); number = I; // Signal that a value has been written. myResetEvent. set (); ChangeEvent. set (); Thread. sleep (0);} payMoneyThread. abort (); getBookThread. abort ();} static void PayMoneyProc () {while (true) {myResetEvent. waitOne (); // myResetEvent. reset (); Console. writeLine ("{0}: quantity {1}", Thread. currentThread. name, number) ;}} static void GetBookProc () {while (true) {ChangeEvent. waitOne (); // ChangeEvent. reset (); Console. writeLine ("{0}: quantity {1}", Thread. currentThread. name, number); Console. writeLine ("----------------------------------------"); Thread. the running result of Sleep (0) ;}}} is as follows:
 

AutoResetEvent and ManualResetEvent

Their usage Declaration is very similar. The Set method sets the signal to the sending status Reset method sets the signal to the non-sending status WaitOne waiting for the signal to be sent. In fact, we can see from the name that a manual,
An automatic method. manual and automatic actually refer to the Reset method processing, as shown in the following example:

Public AutoResetEvent autoevent = new AutoResetEvent (true );
Public ManualResetEvent manualevent = new ManualResetEvent (true );

All signals are sent by default,

Autoevent. WaitOne ();
Manualevent. WaitOne ();

If a thread calls the above method, when the signal is in the sending status, the thread will receive a signal to continue execution. After the difference is called, autoevent. WaitOne () allows only one thread at a time.
When a thread gets a signal (that is, other threads call autoevent. after the Set () method), autoevent automatically sets the signal to not sent, and other threads that call WaitOne only
Wait. That is, autoevent only wakes up one thread at a time. Manualevent can wake up multiple threads, because when a thread calls the set method, other threads that call waitone
The obtained signal can be continued, while manualevent does not automatically set the signal to not be sent. that is, unless manualevent is manually called. reset (). method, manualevent will remain in the signal state, and manualevent will be able to wake up multiple threads at the same time to continue execution. If the preceding program is replaced with ManualResetEvent, You need to reset it after waitone.

Related Article

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.