Usage of autoresetevent

Source: Internet
Author: User
From:

Today, when I read a Demo code, I found that autoresetevent was used repeatedly. I checked the information in msnd and found a very short example on the Internet, this example has helped me understand the usage of autoresetevent and has played a lot of auxiliary functions. It is posted here, hoping to help you understand it:
(The code has been successfully debugged in vs2005. Comments are my understanding and can only serve as a reference)

Using system;
Using system. Threading;

Namespace autoresetevent_examples
{
Class mymainclass
{
// There is no signal at the beginning, which means that the parameter is false.
Const int numiterations = 100; // It doesn't matter how many repeated times are set. It sets 100 for you to see clearly.
Static autoresetevent myresetevent = new autoresetevent (false );
Static int number;

Static void main ()
{
// Create and start a thread.
Thread myreaderthread = new thread (New threadstart (myreadthreadproc ));
Myreaderthread. Name = "readerthread ";
Myreaderthread. Start ();

For (INT I = 1; I <= numiterations; I ++)
{
Console. writeline ("writer thread writing value: {0}", I );
Number = I;

// Sends a signal, indicating that the value has been written into it. Set is a method for sending signals.
Myresetevent. Set ();

// Make some intervals in each loop, which has no other effect and can be commented out.
Thread. Sleep (1000 );
}

// Terminate the reading thread.

Myreaderthread. Abort ();
}

Static void myreadthreadproc ()
{
While (true)
{
// The data is not read by the reader before it is written by the author.
// There must be at least one read before the last read.
Myresetevent. waitone ();
Console. writeline ("{0} reading value: {1}", thread. currentthread. Name, number );
}
}
}
}

I will describe it in the diagram below to make it easier for everyone to understand. Before using autoresetevent, you must perform initialization through examples, for example:
Autoresetevent myresetevent = new autoresetevent (false );
As mentioned in msdn, to set the initial state to terminationTrue
If you want to set the initial status to non-terminatedFalse

Women's understanding
: If this parameter is set to true, that is, this event is returned to automatic reset, then myresetevent. waitone () will be able to get the running right when the program starts; it is equivalent to automatically running myreseteven once the program starts. set ();
Otherwise, it is necessary to wait until myreseteven. Set () runs successfully before myreseteven. waitone () can get the running opportunity;

Here I will make an experiment for you to see that when set to true, we will first see the readerthread reading value running, and then the writer thread wrirting:

If it is set to false, you will find that the write information first appears:

I believe everyone should understand its initial meaning.
The above mentioned myreseteven. Set (). What is this method for? In simple words, it is equivalent to a switch. If the Set () method is not executed, the following
Waitone () will not be able to wait for the notification to be executed, so that the statements after waitone will not be executed, and waitone () will be waiting until set () is executed.
The operation after it is executed,

Let's try again and comment out myreseteven. Set (). The result is that the read information will never appear ,:


As we expected, only the written information is executed.

In combination, autoresetevent provides a toggle similar to condition judgment to control the thread,
If ()
{
......

}

That is to say, if the thread above autoresetevent. Set () is fully executed, the thread under the control of autoresetevent. waitone () will be executed. It's easy !!

I don't know if the female's expression is clear. If you have any questions, please contact us!

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.