Thread synchronization----Manual synchronization

Source: Internet
Author: User

First, ManualResetEvent class

Used to keep a thread in a wait state. There are two common methods:

Set (): Sets the status to signaled.

Restset (): Resets the status to no signal.

WaitOne: Waits for an event object to become signaled.

[STAThread] Public Static voidMain () {ManualResetEvent manre; Manre=NewManualResetEvent (true);//the initial state is a signal            BOOLState = Manre.waitone ( the,true);//in a signaled state, even if the wait time is set, the thread will not wait within the WaitOne methodConsole.WriteLine ("ManualResetEvent after first WaitOne--"+ state);//True//status changed to No signalManre.reset (); State= Manre.waitone ( the,true);//no signal state, the thread waits for a specified length of timeConsole.WriteLine ("ManualResetEvent after second WaitOne--"+ state);//FalseConsole.ReadLine (); }

The ManualResetEvent class also has WaitAny, WaitAll, and the usage can be inferred from the literal meaning.

Second, Autoresetenvet class

Usage is similar to the ManualResetEvent class, with the only difference being in the WaitOne method. After the WaitOne method is executed, the signal state is automatically transformed.

If the example in the previous example is implemented with Autoresetenvet, the following:

1         Public Static voidMain ()2         {3 AutoResetEvent is;4is =NewAutoResetEvent (true);//Initial termination State5Console.WriteLine ("before first WaitOne");6             BOOLState = Are.waitone ( the,true);//does not wait (because the initial state is signaled), then the signal automatically becomes false. 7Console.WriteLine ("After first WaitOne--"+State );8             //status changed to No signal9             //Are.reset ();//you no longer need to manually change the signal status. TenState = Are.waitone ( the,true); OneConsole.WriteLine ("After second WaitOne--"+State ); A console.readline (); -}

Third, the Mutex class

Thread synchronization----Manual 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.