AutoResetEvent of C # thread synchronization

Source: Internet
Author: User

The AutoResetEvent object is used for thread synchronization operations, and the AutoResetEvent class inherits the WaitHandle class.

The AutoResetEvent object has both terminating and non-terminating states, the terminating state is that the thread continues execution, the non-terminating state causes the thread to block, and the set and reset methods can be called to bring the object into the terminating and non-terminating state. AutoResetEvent as the name implies, its object will automatically call reset once the set is called, entering a non-terminating state so that the thread that calls the wait method enters the blocking state.

The WaitOne of the WaitHandle object can cause the current thread to enter a blocking state, waiting for a signal. Execution continues until the current WaitHandle object receives a signal.

The set can send a signal that allows one or more to wait for the thread to continue executing because of the call to WaitOne, and, after experimentation, if there are multiple waiting threads, the set can only allow one thread to continue executing.

Reset can cause the thread to go into a blocking state because it calls WaitOne.

The following example launches two threads and uses AutoResetEvent objects to block or continue.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespacemytest{classProgram {Const intNumiterations =5; //initialstate to be blocked at first, set to false, and set to true at first if it is non-blocking.         StaticAutoResetEvent myresetevent =NewAutoResetEvent (false); Static intNumber=0; Static voidMain (string[] args) {Thread Myreaderthread=NewThread (NewThreadStart (MyReadThreadProc)); Myreaderthread.name="Readerthread";            Myreaderthread.start (); Thread myReaderThread2=NewThread (NewThreadStart (MYREADTHREADPROC2)); Myreaderthread2.name="ReaderThread2";            Myreaderthread2.start (); //Myresetevent.reset (); //set the terminating state to let the WaitOne thread block             for(inti =1; I <= numiterations; i++) {Console.WriteLine ("Writer Thread Writing value: {0}", i); number=i; //sets the event state to the signaled state, allowing one or more waiting threads to continue. Myresetevent.set (); Thread.Sleep ( -);                Myresetevent.set (); //take a break and let threads 1 and 2 have time to executeThread.Sleep ( -);            } myreaderthread.abort ();        Console.ReadLine (); }        Static voidMyReadThreadProc () { while(true)            {                //blocks the current thread until Threading.waithandler receives a signalMyresetevent.waitone (); Console.WriteLine ("Thread1: {0} reading value: {1}", Thread.CurrentThread.Name, number); }        }        Static voidMyReadThreadProc2 () { while(true)            {                //blocks the current thread until Threading.waithandler receives a signalMyresetevent.waitone ();
Console.WriteLine (DateTime.Now.ToString ()); Console.WriteLine ("thread2:{0} reading value: {1}", Thread.CurrentThread.Name, number); } } }}

Execution Result:

As you can see in the results, if there are multiple threads waiting, only one thread continues to execute after each set, and the thread that continues execution is random.

AutoResetEvent of C # thread synchronization

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.