Thread waits for the WaitForSingleObject and Waitformultipleobject of the Waitxxxxx () function series

Source: Internet
Author: User

wait* () function family can realize the detection of events, and the function can realize the detection of single signal and multi-signal.

Maybe if I say so, we might be a little bit confused. No hurry, let's start by introducing two functions.

The first one is for single-signal detection. WaitForSingleObject ()

Function prototypes

DWORD WaitForSingleObject (

HANDLE Hhandle,

DWORD dwmilliseconds

);

hhandle object handle. You can specify a series of objects, such as event job , memory Resourcenotification mutex process , Span style= "COLOR: #333333" >semaphore Thread waitable timer and so on.

dwmilliseconds timing interval, in units of milliseconds (MS) . If you specify a value other than 0, the function is in a wait state until Hhandle the Tagged object is triggered, or the time is up. If dwmilliseconds is 0, the object is not signaled, and the function does not enter a wait state, and it always returns immediately. If dwmilliseconds is INFINITE, the function will not return until the object is signaled.

The execution succeeds, and the return value indicates the event that the function returned is raised. It may be the following values:

wait_abandoned , Wait_object_0 , Wait_timeout , wait_failed .

WaitForSingleObjectfunction is used to detectHhandleevent when the function is called in a thread, the threads are temporarily suspended if the pendingdwmillisecondsin milliseconds, the object that the thread waits for becomes signaled, and the function returns immediately if the time-out has arriveddwmillisecondsmilliseconds, butHhandleThe object pointed to has not yet become signaled, and the function returns. ParametersdwmillisecondsThere are two values that are of special significance:0and theINFINITE. If the0, the function returns immediately;INFINITE, the thread has been suspended untilHhandleThe object that is pointing to becomes signaled.

wait_abandoned : when Hhandle to be Mutex , if you have Mutex This return value is raised when the thread does not release the core object at the end.

Wait_object_0 : The core object has been activated

Wait_timeout : Wait Timeout

wait_failed : An error has occurred that can be passed GetLastError Get the error code

The second one is for multi-signal detection. Waitformultipleobject ()

Function prototypes

DWORD Waitformultipleobject (

DWORD Dwcount,

CONST handle* Phobject,

BOOL fWaitAll,

DWORD Dwmillisecinds

);

dwcount Span style= "COLOR: #333333" > parameter kernel object 1 maximum_wait_objects ( windows 64 between

phobjects parameter is point to kernel object array pointer waitformultipleobjects kernel object kernel object

fWaitAll Parameters tell the function which way you want it to be used. If TRUE is passed for this parameter ,the function will not allow the calling thread to run until all the objects become notified states.

Dwmillisecond s Parameters The function of this parameter is related to its WaitForSingleObject are identical in function. If the specified time is reached while waiting, the function will return anyway. Again, it is common to pass INFINITE for this parameter , but you should be careful when writing code to avoid deadlock situations.

WaitForMultipleObjectsThe return value of the function tells the calling thread why it is being re-dispatched. The possible return value iswait_failedand theWait_timeout, the effect of these two values is very clear. If thef WaitAll parameter passing TRUE, and all objects become notification states, the return value isWait_object_0. If thefWaitAllPassFALSE, the function returns once any one object becomes a notified state. In this case, you may want to know which object becomes the notification state. The return value isWait_object_0with (Wait_object_0 + dwCount-1) is a value between. In other words, if the return value is notWait_timeout, nor iswait_failed, you should subtract from the return valueWait_object_0. The resulting number is as a secondparameter PassingGiveWaitForMultipleObjectsthe handleArraythe index in the. The index indicates which object has changed to the notified state.

The basic introduction to two functions looks like a headache, and then we use the code to help us understand the two functions.

Multi-signal detection

 #include <windows.h> #include <iostream>using namespace std; DWORD WINAPI print_thread (lpvoid data) {cout<< "start thread \ n" << (int) data+1;for (int index=0;index<50* (int) DATA+1);        index++)//depending on the number of cycles to see the thread health more clearly, {cout<< (int) data+1<< "";    Sleep (100);    } cout<< "Thread" << (int) data+1<< "execution complete \ n";  Return (DWORD) data;    int main () {HANDLE threadhandle[3];     DWORD Threadid[3];            for (int i=0;i<3;i++) {threadhandle[i]=createthread (NULL, 0, Print_thread,    (LPVOID) I, 0, &threadid[i]);        } for (int index=0;index<25; index++) {cout<< "4" << "";   Sleep (100);//wait 100 milliseconds} cout<< "Main thread end, go to wait" <<endl;     WaitForMultipleObjects (3,threadhandle,true,infinite); for (int i=0;i<3;i++) CloseHandle (Threadhandle[i]);//Turn off thread handle return 0;} 


Running the program you will find that when the main thread finishes running, it waits for the child thread to finish until the end

If you will WaitForMultipleObjects (3,threadhandle,true,INFINITE), this sentence will be commented out and you'll notice that the main thread ends up exiting the program directly. Instead of waiting for a child thread.


If you will WaitForMultipleObjects (3,threadhandle,true,INFINITE), and the third parameter inside is changed to False, then the program will be executed by any one of the child threads. The program exits. And true is to wait for all child threads to end.

Here only shows the multi-signal detection, single signal but the same way, very simple, we can try.

Thread waits for the WaitForSingleObject and Waitformultipleobject of the Waitxxxxx () function series

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.