WaitForMultipleObjects等待多个事件对象
2011-06-28 10:34
519 查看
函数原型:
DWORD WaitForMultipleObjects(
DWORD nCount,
// number of handles in array
CONST HANDLE *lpHandles, // object-handle
array
BOOL fWaitAll,
// wait option
DWORD dwMilliseconds
// time-out interval
);
该函数功能强大,几乎可以处理Windows下的所有事件,但在处理多个事件对象时,比较容易出错。
MSDN:
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
fWaitAll 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.
问题:
当fWaitAll变量赋值FALSE时,若有多个对象在dwMilliseconds内同时响应,则函数只返回其中一个对象(通常是数组lpHandles中序号最小的)并修改其状态,而不改变其它对象的状态。这将导致序号较小的对象频繁响应,而序号较大的对象得不到处理。本人在coding时,遇到过这种问题。
解决方案:
可采取循环使用双WaitForMultipleObjects函数处理多对象等待问题。
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
DWORD dwRet = 0;
int nIndex = 0;
while(1)
{
dwRet =
WaitForMultipleObjects(nCount,pHandles,false,INFINITE);
switch(dwRet)
{
case WAIT_TIMEOUT:
break;
case WAIT_FAILED:
return 1;
default:
{
nIndex = dwRet -
WAIT_OBJECT_0;
ProcessHanlde(nIndex++);
//同时检测其他的事件
while(nIndex <
nCount)
{
dwRet =
WaitForMultipleObjects(nCount -
nIndex,&pHandles[nIndex],false,0);
switch(dwRet)
{
case
WAIT_TIMEOUT:
nIndex
= nCount; //退出检测,因为没有被触发的对象了.
break;
case
WAIT_FAILED:
return
1;
default:
{
nIndex
= dwRet - WAIT_OBJECT_0;
ProcessHanlde(nIndex++);
}
break
}
}
}
break;
}
}
return 0;
}
DWORD WaitForMultipleObjects(
DWORD nCount,
// number of handles in array
CONST HANDLE *lpHandles, // object-handle
array
BOOL fWaitAll,
// wait option
DWORD dwMilliseconds
// time-out interval
);
该函数功能强大,几乎可以处理Windows下的所有事件,但在处理多个事件对象时,比较容易出错。
MSDN:
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
fWaitAll 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.
问题:
当fWaitAll变量赋值FALSE时,若有多个对象在dwMilliseconds内同时响应,则函数只返回其中一个对象(通常是数组lpHandles中序号最小的)并修改其状态,而不改变其它对象的状态。这将导致序号较小的对象频繁响应,而序号较大的对象得不到处理。本人在coding时,遇到过这种问题。
解决方案:
可采取循环使用双WaitForMultipleObjects函数处理多对象等待问题。
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
DWORD dwRet = 0;
int nIndex = 0;
while(1)
{
dwRet =
WaitForMultipleObjects(nCount,pHandles,false,INFINITE);
switch(dwRet)
{
case WAIT_TIMEOUT:
break;
case WAIT_FAILED:
return 1;
default:
{
nIndex = dwRet -
WAIT_OBJECT_0;
ProcessHanlde(nIndex++);
//同时检测其他的事件
while(nIndex <
nCount)
{
dwRet =
WaitForMultipleObjects(nCount -
nIndex,&pHandles[nIndex],false,0);
switch(dwRet)
{
case
WAIT_TIMEOUT:
nIndex
= nCount; //退出检测,因为没有被触发的对象了.
break;
case
WAIT_FAILED:
return
1;
default:
{
nIndex
= dwRet - WAIT_OBJECT_0;
ProcessHanlde(nIndex++);
}
break
}
}
}
break;
}
}
return 0;
}
相关文章推荐
- WaitforMultipleObjects和事件对象
- 用ACE的Reactor模式实现网络通讯时,ACE内部用WSAEventSelect函数把网络事件与一个事件对象关联起来,目的是为了后面用WaitForMultipleObjects函数统一处理。
- WaitForMultipleobjects 最大等待对象限制
- WaitForMultipleObjects将用于等待多个对象变为有信号状态
- WaitForMultipleObjects等待超过MAXIMUM_WAIT_OBJECTS个的内核对象
- delphi 中使用WaitForMultipleObjects等待线程执行,再执行后续代码
- 主线程使用WaitForSingleObject和MsgWaitForMultipleObjects等待线程返回值
- _beginthreadex使用WaitForMultipleObjects等待退出的问题
- delphi 中使用WaitForMultipleObjects等待线程执行,再执行后续代码
- 如何用WaitForMultipleObjects获取所有被同时触发的内核对象
- 等待线程退出 MsgWaitForMultipleObjects和WaitForSingleObject
- 改进的延时函数Delay(使用MsgWaitForMultipleObjects等待消息或超时的到来)
- 临界区、互斥对象、WaitForSingleObject、WaitForMultipleObjects
- 关于WaitForMultipleObjects函数监测事件状态的问题
- 主线程利用MsgWaitForMultipleObjects等待子线程结束时,同时处理子线程发送的窗口消息
- delphi 中使用WaitForMultipleObjects等待线程执行,再执行后续代码
- WaitForSingleObject、WaitForMultipleObjects、CreateThread
- WaitForSingleObject 和 WaitForMultipleObjects函数 (让线程挂起等待事件)
- WaitForMultipleObjects
- 四极管:WaitForMultipleObjects的疑惑