WaitForMultipleObjects的使用

WaitForMultipleObjects 函数说明
2007-04-22 22:14

PS:这个函数搞得我头有点大

the WaitForMultipleObjects function returns when one of the following occurs:

  • either any one or all of the specified objects are in the signaled state.
  • the time-out interval elapses.

to enter an alertable wait state, use the waitformultipleobjectsex function.

dword WaitForMultipleObjects (
dword
ncount, // number of handles in array
const handle *lphandles, // object-handle array
bool bwaitall, // wait option
dword dwmilliseconds // time-out interval
);
parameters
ncount
[in] specifies the number of object handles in the array pointed to by lphandles. the maximum number of object handles is maximum_wait_objects.
lphandles
[in] pointer to an array of object handles. for a list of the object types whose handles can be specified, see the following remarks section. the array can contain handles to objects of different types. it may not contain the multiple copies of the same handle.

if one of these handles is closed while the wait is still pending, the function's behavior is undefined.

windows nt/2000/xp: the handles must have synchronize access. for more information, see standard access rights .

windows 95/98/me: no handle may be a duplicate of another handle created using duplicatehandle.

bwaitall
[in] specifies the wait type. if true, the function returns when the state of all objects in the lphandles array is signaled. if false, the function returns when the state of any one of the objects is set to signaled. in the latter case, the return value indicates the object whose state caused the function to return.
dwmilliseconds
[in] specifies the time-out interval, in milliseconds. the function returns if the interval elapses, even if the conditions specified by the bwaitall parameter are not met. if dwmilliseconds is zero, the function tests the states of the specified objects and returns immediately. if dwmilliseconds is infinite, the function's time-out interval never elapses.
return values

if the function succeeds, the return value indicates the event that caused the function to return. this value can be one of the following.

valuemeaning
wait_object_0 to (wait_object_0 + ncount – 1)if bwaitall is true, the return value indicates that the state of all specified objects is signaled.

if bwaitall is false, the return value minus wait_object_0 indicates the lphandles array index of the object that satisfied the wait. if more than one object became signalled during the call, this is the array index of the signalled object with the smallest index value of all the signalled objects.

wait_abandoned_0 to (wait_abandoned_0 + ncount – 1)if bwaitall is true, the return value indicates that the state of all specified objects is signaled and at least one of the objects is an abandoned mutex object.

if bwaitall is false, the return value minus wait_abandoned_0 indicates the lphandles array index of an abandoned mutex object that satisfied the wait.

wait_timeoutthe time-out interval elapsed and the conditions specified by the bwaitall parameter are not satisfied.
f the function fails, the return value is wait_failed. to get extended error information, call getlasterror .
remarks

the waitformultipleobjects function determines whether the wait criteria have been met. if the criteria have not been met, the calling thread enters the wait state. it uses no processor time while waiting for the criteria to be met.

when bwaitall is true, the function's wait operation is completed only when the states of all objects have been set to signaled. the function does not modify the states of the specified objects until the states of all objects have been set to signaled. for example, a mutex can be signaled, but the thread does not get ownership until the states of the other objects are also set to signaled. in the meantime, some other thread may get ownership of the mutex, thereby setting its state to nonsignaled.

the function modifies the state of some types of synchronization objects. modification occurs only for the object or objects whose signaled state caused the function to return. for example, the count of a semaphore object is decreased by one. when bwaitall is false, and multiple objects are in the signaled state, the function chooses one of the objects to satisfy the wait; the states of the objects not selected are unaffected.

the waitformultipleobjects function can specify handles of any of the following object types in the lphandles array:

  • change notification
  • console input
  • event
  • job
  • mutex
  • process
  • semaphore
  • thread
  • waitable timer
use caution when calling the wait functions and code that directly or indirectly creates windows. if a thread creates any windows, it must process messages. message broadcasts are sent to all windows in the system. a thread that uses a wait function with no time-out interval may cause the system to become deadlocked. two examples of code that indirectly creates windows are dde and com coinitialize. therefore, if you have a thread that creates windows, use msgwaitformultipleobjects or msgwaitformultipleobjectsex, rather than waitformultipleobjects.
  

HANDLE hWait[4];
 hWait[0] = m_hWait1;
 hWait[1] = m_hWait2;
 hWait[2] = m_hWait3;
 hWait[3] = m_hWait4;

 DWORD dwEvent = ::WaitForMultipleObjects(4,hWait,TRUE,INFINITE);

 switch(dwEvent)
 {
 case WAIT_OBJECT_0:
  {
   AfxMessageBox("1");
  }
  break;

 case WAIT_OBJECT_0+1:
  {
   AfxMessageBox("2");
  }
  break;

 case WAIT_OBJECT_0+2:
  {
   AfxMessageBox("3");
  }
  break;

 case WAIT_OBJECT_0+3:
  {
   AfxMessageBox("4");
  }
  break;

 case WAIT_TIMEOUT:
  {
  }
  break;
 default:
  break;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值