Thread Synchronization-eventwaithandle & autoresetevent & manualresetevent

Source: Internet
Author: User
Eventwaithandle provides two methods: Automatic (eventresetmode. autoreset) and manual (eventresetmode. manualreset). The main difference is whether to automatically set the synchronization status to a non-terminated State to block the thread. Static void eventwaithandletest2 (eventresetmode Mode)
{
Eventwaithandle R1 = new eventwaithandle (false, mode );

Thread T1 = new thread (delegate ()
{
While (true)
{
R1.waitone ();

Console. writeline ("1 ");
Thread. Sleep (1000 );

// R1.reset ();
// R1.set ();
}
}
);

T1.start ();
R1.set ();
}

When we use eventwaithandletest2 (eventresetmode. autoreset); To call this method, we find that the loop body does not continue after execution, and the thread is blocked. While eventwaithandletest2 (eventresetmode. manualreset) is not blocked during the call, and the loop body is infinitely executed. Therefore, we can distinguish the two States. The automatic mode will automatically set the synchronization status to non-terminated, but the manual mode will not. When the status is automatic, we delete r1.set (); the comment character (//) of the row, and reset the status to the terminated status. Then, the loop body performs the next round of execution automatically. manually, when we delete the comment row of r1.reset ();, the synchronization status will be reset to the non-terminated status, so the thread body will naturally be blocked and the execution of the loop body will not continue.

Autoresetevent and manualresetevent of. NET Framework 2.0 inherit from eventwaithandle, not from waithandle in 1.x. These two classes only overwrite the constructor. Public autoresetevent (bool initialstate): Base (initialstate, eventresetmode. autoreset)
{
}

Public manualresetevent (bool initialstate): Base (initialstate, eventresetmode. manualreset)
{
}

Next, let's write another example. Let two threads run alternately. Using system;
Using system. Collections. Generic;
Using system. text;
Using system. diagnostics;
Using system. Threading;

Namespace consoleapplication1
{
Class Program
{
Static void eventwaithandletest (eventresetmode Mode)
{
Eventwaithandle R1 = new eventwaithandle (false, mode );
Eventwaithandle r2 = new eventwaithandle (false, mode );

Thread T1 = new thread (delegate ()
{
While (true)
{
R1.waitone ();

Console. writeline ("1 ");
Thread. Sleep (1000 );

If (mode = eventresetmode. manualreset) r1.reset ();
R2.set ();
}
}
);

Thread t2 = new thread (delegate ()
{
While (true)
{
R2.waitone ();
Console. writeline ("2 ");

If (mode = eventresetmode. manualreset) r2.reset ();
R1.set ();
}
}
);

T1.start ();
T2.start ();

R1.set ();
}

Static void main ()
{
Eventwaithandletest (eventresetmode. autoreset );

Console. writeline ("press any key to exit ...");
Console. readkey (true );
}
}
}

Why should we use two synchronization objects? Imagine if you use one to execute the T1 loop r1.set (); when the code is executed, the time segment allocated by the CPU to t1 may not have timed out, so the execution right will not be handed over to T2, because it will continue to execute the T1 loop body code, this problem can be avoided by using two.

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.