C + + win development WaitForSingleObject function

Source: Internet
Author: User

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

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.