The WINAPI WaitForSingleObject function is often used when C + + involves multiple threads
But because a lot of online information directly refer to the official website of the English translation of many places more obscure difficult to understand
Individuals have also done some tests to figure out the use of this function
First look at the instructions on Baidu
The WaitForSingleObject function is used to detect the signal state of the Hhandle event, and when the function is called in a thread, it is temporarily suspended, and if the thread waits for the object to become signaled in the suspended dwmilliseconds millisecond, the function immediately returns If the time-out has reached dwmilliseconds milliseconds, but the object pointed to by Hhandle has not become signaled, the function returns. The parameter dwmilliseconds has two values with special meaning: 0 and infinite. If 0, the function returns immediately, and if infinite, the thread is suspended until the object pointed to by Hhandle becomes signaled state.
I personally understand that the WaitForSingleObject function passed in the Hhandle object is returned if it is in a signaled state.
If there is no signal then the suspend time is determined by the dwmilliseconds parameter
The key is the concept of resetting reset
The reset reset referred to here means that the Hhandle object is placed in a signal-free State
Take a look at the Create event function below
HANDLECreateEvent (lpsecurity_attributeslpeventattributes,//Security PropertiesBOOLbmanualreset,//Reset ModeBOOLbinitialstate,//Initial StateLPCTSTRLpname//object name);
The second parameter is used to specify the reset mode of the
If true, manual reset mode
When a thread dispatches to the WaitForSingleObject function Hhandle object is signaled state
Does not modify the signal state of the Hhandle object
If False, Reset automatically
The thread calls the WaitForSingleObject function automatically when the Hhandle object is set to No signal
the following 2 functions can be used to set the signal state of a handle object
ResetEvent (__in HANDLE hevent)//reset is set to no signal status
SetEvent (__in HANDLE hevent)//set to signaled state
C + + win development WaitForSingleObject function