Thread synchronization--kernel objects implement thread synchronization--wait function

Source: Internet
Author: User

1 to implement thread synchronization for kernel objects, we have to mention three points:2 1most kernel objects have both triggers and no triggering of two states3 For example: process, thread, job, file stream, event, waiting timer, semaphore, mutex4 2wait function: The wait function causes the thread to voluntarily enter the wait state until the specified kernel object becomes a trigger state .5 said waiting for our favorite, because this will not waste our precious CPU time. 6 3for an auto-reset object, when the object is triggered, the function is automatically detected (manually resetting the object to trigger Yes, and the function can detect it).7 and starts execution, but the function changes the event to a non-triggering state before returning it. 8 9 Here are some of the most common wait functions:Ten 1): One DWORD WaitForSingleObject (HANDLE Hhandle, DWORD dwmilliseconds); A The first parameter, Hhandle, is used to identify the kernel object to wait for, and the second person parameter, dwmilliseconds, identifies how long it takes to wait for a volunteer.  -  - The following function tells the system that the thread wants to wait until the thread is in the process of terminating or hprocess the identified object becomes the triggering state the WaitForSingleObject (hprocess,infinite); -  - For The return problem of the wait function, let's look at the following code: -DWORD DW = WaitForSingleObject (hprocess, the) ; +  - Switch(DW) + { A  CaseWAIT_OBJECT_0: at     //The thread waits for the object to be triggered, which means the thread has not yet reached 5000 waiting time to start working. -      Break; -  -  Casewait_timeout: -     //The thread waits for a timeout, and the thread does not wait until the object is triggered at 5000, but the time is up and the thread starts to work. -      Break; in  -  Casewait_failed: to     //The thread has an invalid handle, and the consequences may not be predictable. +      Break; - } the  * 2) $ DWORD WaitForMultipleObjects (Panax Notoginseng DWORD ncount, -CONST *HANDLE phobjects, the BOOL bWaitAll, + DWORD dwmilliseconds); A The first parameter, ncount, indicates that we want to detect the number of kernel objects, which must be at 1 to maximum_wait_objects Max . the I believe it's enough already, and it's not going to work that way for so long . + The second parameter, phobjects, is a pointer to an array of kernel object handles - the third parameter, bWaitAll, is the one that tells the function which way we want to use. If this function passes in TRUE, $ The function will not allow the calling thread to execute until all kernel objects have been triggered.  $ The fourth parameter dwmilliseconds is the same as the second parameter of WaitForSingleObject, how long we wait, - of course, you can also pass in infinite to be willing to wait until the condition is met.  -  the As for the return value of WaitForMultipleObjects, let's take a look at the code wow: - WuyiHANDLE h[3]; theh[0] =HProgress1; -h[1] =HProgress2; Wuh[2] =HPROGRESS3; -  AboutDWORD DW = WaitForMultipleObjects (3, H,false, the) ; $ Switch(DW) - { -  Casewait_failed: -     //pass in an invalid handle, the consequence is not much to say Ha A      Break; +  the  Casewait_timeout: -     //The thread waits for a timeout, and the thread does not wait until the object is triggered at 5000, but the time is up and the thread begins to work. $      Break; the  the  CaseWait_object_0 +0: the     //Object H[0] is the trigger state the      Break; -  in  CaseWait_object_0 +1: the     //Object H[1] is the trigger state the      Break; About  the  CaseWait_object_0 +2: the     //Object H[2] is the trigger state the      Break; + } - //This also has drawbacks, such as h[0] and h[1] at the same time as the trigger, the result h[0] finished processing and returned the The following code DWORD WINAPI threadfunone (PVOID pvparam) thread in the comment line run, you can experience. Bayi  the#include"windows.h" the#include"iostream" - using namespacestd; - LongG_x =0 ; the  the //defining an Event object 1 the //HANDLE g_hevent1;  the //  - //defining an Event object 2 the //HANDLE G_hevent2;  the  theHANDLE h[2];94  the  the  the //defining thread Functions 198 DWORD WINAPI threadfunone (PVOID pvparam); About  - //Defining thread Functions 2101 DWORD WINAPI threadfuntwo (PVOID pvparam);102 103 intMain ()104 { the 106 //to create a manually reset event object107h[0] =CreateEvent (null,false,true,null);108 109 //to create a manually reset event object theh[1] =CreateEvent (null,false,true,null);111  the //set event to not trigger state113 //resetevent (g_hevent); the  the //Create thread 1 theHANDLE Hthreadone = CreateThread (NULL,0, Threadfunone,0,0, NULL);117 CloseHandle (hthreadone);118 119 //Create thread 2 -HANDLE hthreadtwo = CreateThread (NULL,0, Threadfuntwo,0,0, NULL);121 CloseHandle (hthreadtwo);122 123 //let the main thread hang first to make sure that the other threads perform the completion124Sleep ( +);  thecout<<g_x<<Endl;126 return 0 ;127 } - 129 DWORD WINAPI threadfunone (PVOID pvparam) the {131WaitForMultipleObjects (2, H,false,infinite);//operation result is 2 the //waitformultipleobjects (2,h,true,infinite);//operation result is 1133g_x++;134 135 return 0;136 }137 138 DWORD WINAPI threadfuntwo (PVOID pvparam)139 { $Sleep ( $);141WaitForSingleObject (h[1],infinite);142g_x++; 143 144 return 0;145 }146  

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.