Waitforsingleobject and waitformultipleobjects in Windows

Source: Internet
Author: User

The waitforsingleobject function checks the current state of the specified object. if the object's state is nonsignaled, the calling thread enters the wait state. it uses no processor time while waiting for the object state to become signaled or the time-out interval to elapse.

Usage: controls when a thread executes a function at an interval.

1: control when the thread is executed

In the following thread function, the following for loop is executed only when g_event is set to a signal state. Because g_event is a global variable, we can use g_event in other threads. setevent controls this thread.

Cevent g_event;

Uint cflushdlg: mythreadproc (lpvoid pparam)

{

Waitforsingleobject (g_event, infinite );

For (;;)

{

............

}

Return 0;

}

2: execute a certain thread function body at intervals

In this thread function, you can set mt_interval to control how often the function body of this thread is executed. When the event is in a non-signal state, the function is executed once every mt_interval, when the event is set to a signal state, the thread completes execution.

Uint cflushdlg: mythreadproc (lpvoid pparam)

{

While (waitforsingleobject (g_event, mt_interval )! = Wait_object_0)

{

..................

}

Return 0;

}

Waitformultipleobject

DWORD waitformultipleobject (DWORD dwcount, const handle * phobject, bool fwaitall, DWORD dwmillisecinds );

The dwcount parameter specifies the number of kernel objects that you want the function to view. This value must be between 1 and maximum_wait_objects (defined as 64 in the Windows header file.

The phobjects parameter is a pointer to the array of the kernel object handle. The waitformultipleobjects 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 way is to let the thread enter the waiting state until all specified kernel objects change to the notified State.

The fwaitall parameter tells the function how 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 objects are in the notified State.

Dwmil liseconds: this parameter serves exactly the same purpose as waitforsingleobject. If the specified time is reached while waiting, the function will return at any time. Similarly, infinite is usually passed for this parameter, but be careful when writing the code to avoid deadlocks.

The Return Value of the waitformultipleobjects function tells the calling thread why it is rescheduled. Possible return values are wait_failed and wait_timeout. The functions of these two values are clear. If the value of F waitall is true, and all objects are in the notified State, the return value is wait_object_0. If false is passed for fwaitall, this function returns if any object changes to the notified State. In this case, you may want to know which object has changed to the notified State. The return value is a value between wait_object_0 and (wait_object_0 + dwCount-1. In other words, if the returned value is not wait_timeout or wait_failed, wait_object_0 should be subtracted from the returned value. The resulting number is the index in the handle array passed to waitformultipleobjects as the second parameter. This index indicates which object has changed to the notified status.

Add a simple example so that you can better understand this function:

(Note: The following example also contains the thread priority settings. Use the Win32 console in vc6.0 for debugging .)

# Include <windows. h>

# Include <stdio. h>

DWORD winapi threadidleproc (lpvoid lpparam)

{

Int I = 0;

While (I ++ <10)

{

Printf ("Idle thread is running./N ");

}

Return 0;

}

DWORD winapi threadnormalproc (lpvoid lpparam)

{

Int I = 0;

While (I ++ <10)

{

Printf ("normal thread is running./N ");

}

Return 0;

}

Int main (INT argc, char * rgv [])

{

Handle H [2];

DWORD dwthreadid;

H [0] =: createthread (null, 0, threadidleproc, null, create_suincluded, & dwthreadid );

: Setthreadpriority (H [0], thread_priority_idle); // sets the thread priority.

: Resumethread (H [0]); // wake up

H [1] =: createthread (null, 0, threadnormalproc, null, create_suincluded, & dwthreadid); // when create_suincluded is set to 0, it indicates immediate execution, the following two functions are not required.

: Setthreadpriority (H [1], thread_priority_normal );

: Resumethread (H [1]);

DWORD dw;

// When the values of 3rd and 4 are set to true and infinite, the system returns the result only after all threads have completed execution,

// When it is set as follows, as long as one thread is in the trusted state, it will return

DW =: waitformultipleobjects (2, H, false, 5000 );

Switch (DW)

{

Case wait_failed:

Break;

Case wait_timeout:

Break;

Case wait_object_0:

Break;

Case wait_object_0 + 1:

Break;

Default :;

}

: Closehandle (H [0]);

: Closehandle (H [1]);

Return 0;

}

Z to slave

 

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.