Multi-thread synchronization-Detailed description of the use of the waitformultipleobjects () function
Source: Internet
Author: User
Waitformultipleobjects is a very powerful function in windows. It can wait for almost all kernel objects in Windows (for descriptions and examples of this function, see msdn ,). However, this function requires some skills in usage. Prototype: DWORD waitformultipleobjects (<br/> DWORD ncount, <br/> const handle * lphandles, <br/> bool bwaitall, <br/> DWORD dwmilliseconds <br/> ); <br/>When waitformultipleobjects waits for Multiple kernel objects, if its bwaitall parameter is set to false. The return value minus wait_object_0 is the serial number of the lphandles array. If multiple kernel objects are triggered at the same time, this function returns only the one with the smallest serial number. If this parameter is set to true, the system waits until all semaphores are executed. (False: Execute down when one of the semaphores is valid) Here, we can get all kernel objects that are triggered at the same time. For example, we need to process data from the completion port, database, and the waiting timer in a thread. A typical implementation method is to use waitformultipleobjects to wait for all these events. If the port is completed, the data volume sent from the database is very large, and the timer time can only be several dozen milliseconds. The probability of these events being triggered at the same time is very high. We do not want to discard any triggered events. So how can this process be efficiently implemented? Msdn has a very important description, which can be said to be the essence of waitformultipleobjects usage: The function modifies the state of some types of synchronization objects. modification occurs only for the object or objects whose signaled state caused the function to return. for example, the count of a semaphore object is decreased by one. when bwaitall is false, and multiple objects are in the signaled state, the function chooses one of the obje CTS to satisfy the wait; the states of the objects not selected are unaffected. When Multiple kernel objects are triggered, waitformultipleobjects selects the return with the smallest serial number. Waitformultipleobjects only changes the state of the kernel object returned by waitformultipleobjects. Another problem occurs here. If the object with the smallest serial number is frequently triggered, kernel objects with a higher serial number will not be processed. To solve this problem, the dual waitformultipleobjects detection mechanism can be used. See the following example: DWORD winapi threadproc (lpvoid lpparameter) <br/>{< br/> DWORD dwret = 0; <br/> int nindex = 0; <br/> while (1) <br/>{< br/> dwret = waitformultipleobjects (ncount, phandles, false, infinite); <br/> switch (dwret) <br/>{< br/> case wait_timeout: <br/> break; <br/> case wait_failed: <br/> return 1; <br/> default: <br/>{< br/> nindex = dwret-wait_object_0; <br/> processhanlde (nindex ++ ); <br/> // detect other events simultaneously <br/> while (nindex <ncount) // total number of ncount event objects <br/>{< br/> dwret = waitformultipleobjects (ncount-nindex, & phandles [nindex], false, 0 ); <br/> switch (dwret) <br/> {<br/> case wait_timeout: <br/> nindex = ncount; // exit detection, because no trigger object exists. <br/> break; <br/> case wait_failed: <br/> return 1; <br/> default: <br/>{< br/> nindex = dwret-wait_object_0; <br/> processhanlde (nindex ++ ); <br/>}< br/> Break <br/>}< br/> break; <br/>}< br/> return 0; <br/>}< br/>Msdn has another sentence for the return value of this function: return values if the function succeeds, the return value indicates the event that caused the function to return. this value can be one of the following. valuemeaning wait_object_0 to (wait_object_0 + ncount-1) If bwaitall is true, the return value indicates that the state of all specified objects is signaled. if bwaitall is false, the return value minus wait_object_0 indicates The lphandles array index of the object that satisfied the wait. if more than one object became signalled during the call, this is the array index of the signalled object with the smallest index value of all the signalled objects. wait_abandoned_0 to (wait_abandoned_0 + ncount-1) If bwaitall is true, the return value indicates that the state of all specified objects is signaled and at least one Of the objects is an abandoned mutex object. if bwaitall is false, the return value minus wait_abandoned_0 indicates the lphandles array index of an abandoned mutex object that satisfied the wait. wait_timeoutthe time-out interval elapsed and the conditions specified by the bwaitall parameter are not satisfied. if the return value is successful, the return value indicates that the event causes the function to return. This value can be one of the following. Valuemeaning wait_object_0 to (wait_object_0 + ncount-1 If bwaitall is true), the return value indicates the status signals of all specified objects. If bwaitall is false, the return value minus the value not wait_object_0 indicates waiting for the satisfaction index of the objects in the lphandles array. If multiple objects become CITIC during a call, this is an array index of the signal object with the minimum index value of all signal objects. Wait_abandoned_0 to (wait_abandoned_0 + ncount-1 If bwaitall is true), the returned value indicates the status of all specified objects, at least one of the implied objects, and is an obsolete mutex object. If bwaitall is false, the return value minus wait_abandoned_0 indicates waiting for the satisfaction index of an discarded mutex object in the lphandles array. The wait_timeoutthe timeout interval has passed, and the conditions specified by the bwaitall parameter cannot be met.
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