Waitforsingleobject function usage

Source: Internet
Author: User

The waiting function enables the thread to voluntarily enter the waiting state until a specific kernel object changes to the notified State. The most common waiting functions are waitforsingleobject:

DWORD waitforsingleobject (handle hobject, DWORD dwmilliseconds );

When the thread calls this function, the first parameter hobject identifies a kernel object that can be notified or not notified. The second parameter, dwmilliseconds, allows this thread to specify how long it will wait to wait for the object to become notified. Call the following function to tell the system that the calling function is waiting until the process identified by the h p r o c e s handle is terminated:

Waitforsingleobject (hprocess, infinite );

The second parameter tells the system that the calling thread is willing to wait forever (unlimited time) until the process stops running.

Generally, I n f I n I t e is passed to wa I t f o r I n g l e o B j e c t as the second parameter, however, any value can be passed (in milliseconds ). By the way, I n f I n I t e has been defined as 0 x f (or-1 ). Of course, transferring I n f I n I t e is dangerous. If the object never changes to the notified State, the calling thread will never be awakened and it will always be in the Deadlock State,

However, it will not waste valuable c p u time.

The following example shows how to use a timeout value instead of I n f I n I t e to call wa I t f o r s I n g l e o B j e c t:

Dword dw = waitforsingleobject (hprocess, 5000 );

Switch (DW)

{

Case wait_object_0:

// The process terminated.

Break;

Case wait_timeout:

// The process did not terminate within 5000 milliseconds.

Break;

Case wait_failed:

// Bad call to function (invalid handle ?)

Break;

}

The aboveCodeThe system is notified that the calling thread should not change to a schedulable State before a specific process is terminated or before the end of the 5 0 0 0 m s time. Therefore, if the process is terminated

Function call will return within 5 0 0 0 m S. If the process has not ended, it will return within 5 0 0 0 m s. Note that 0 cannot be transferred for d w m I l I s e c o n d. If 0 is passed

, Wa I t f o r s I n g l e o B j e c t function will always return immediately.

The return value of wa I t f o r s I n g l e o B j e c t can indicate why the calling thread changes to the schedulable status again. If the waiting object changes to the notified State, the returned value is wa I t _ o B j e c T _ 0.

. If the set timeout has expired, the returned value is wa I t _ t I m e o u t. If an error value (such as an invalid handle) is passed to wa I t f o r s I n g l eo B j e c t, the returned value is wa I

T _ fa I l e d (for details, you can call G E T L A S T E R o r ).

Below is the function wa I t f o r m u l t I p L e o B j e c t s and Wa I t f o r s I n g l e o B j E c t functions are very similar, the difference is that it allows the calling thread to view the notified status of several kernel objects at the same time:

DWORD waitformultipleobjects (DWORD dwcount,

Const handle * phobjects,

Bool fwaitall,

DWORD dwmilliseconds );

The d w c o u n t parameter is used to specify the number of kernel objects to be viewed by the function. This value must be 1 and m a x I m u m _ wa I t _ o B j e c T S (defined as 6 4 in the WI n d o W S header file). P H O

The B j e c T S parameter is the pointer to the array of the kernel object handle.

The wa I t f o r m u l t I p L e o B j e c T S function can be used in two different ways. One way is to let the thread enter the waiting state until any of the specified kernel objects changes to the notified State. Another

The thread enters the waiting state until all specified kernel objects change to the notified State. The F wa I tal l parameter tells the function how you want it to be used. If t r u e is passed for this parameter

Before all objects are notified, this function will not allow the calling thread to run.

The function of the d w m I l I s e c o n d S parameter and Its Role in wa I t f o r s I n g l e o B j e c t identical. If the specified time is reached while waiting, the function will return at any time. Similarly

Generally, I n f I n I t e is passed for this parameter, but be careful when writing the code to avoid deadlocks.

The Return Value of the Wa I t f o r m u l t I p L e o B j e c T S function tells the calling thread why it is rescheduled. Possible return values are wa I t _ fa I l e d and wa I t _ t I m e o u t.

It is clear. If t r u e is passed for the F wa I tal l parameter and all objects are in the notified State, the returned value is wa I t _ o B j e c T _ 0. If f wa I t a l transmits fa L S E, once

If any object changes to the notified status, this function returns. In this case, you may want to know which object has changed to the notified State. The returned values are wa I t _ o B j e c T _ 0 and (wa I t _ o B j e c

A value between T _ 0 + d w c o u n t-1. In other words, if the returned value is not wa I t _ t I m e o u t, it is not wa I t _ fa I l e d, then we should subtract wa I t _ o B j e c T _ 0 from the return value.

. The resulting number is the index in the handle array passed to wa I t f o r m u l t I p L e o B j e c t s as the second parameter. This index indicates which object has changed to the notified status. The following are some examples of this situation:

Sample Code:

Handle H [3];

H [0] = hprocess1;

H [1] = hprocess2;

H [2] = hprocess3;

Dword dw = waitformultipleobjects (3, H, false, 5000 );

Switch (DW)

{

Case wait_failed:

// Bad call to function (invalid handle ?)

Break;

Case wait_timeout:

// None of the objects became signaled within 5000 milliseconds.

Break;

Case wait_object_0 + 0:

// The process identified by H [0] (hprocess1) terminated.

Break;

Case wait_object_0 + 1:

// The process identified by H [1] (hprocess2) terminated.

Break;

Case wait_object_0 + 2:

// The process identified by H [2] (hprocess3) terminated.

Break;

}

If f wa I t a l parameter is passed, wa I t f o r m u l t I p L e o B j e c t s scans the handle array from index 0, at the same time, the first object that has been notified is in the wait state for termination. This may produce

Generate some results that you don't want. For example, by passing three process handles to the function, your thread will wait for the three sub-processes to terminate. If the process with the index 0 in the array stops running, wa I t f o r m

U l t I p L e o B j e c t s will return. At this time, the thread can do anything it needs, and then loop repeatedly, waiting for another process to stop running. If the thread passes the same three handles, the function immediately

Returns wa I t _ o B j e c T _ 0 again. The Code cannot run correctly unless you delete the handle that has received the notification.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/bichenggui/archive/2009/09/14/4551453.aspx

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.