当多个句柄同时发出信号时,WaitForMultipleObject的行为 [英] Behavior of WaitForMultipleObjects when multiple handles signal at the same time

查看:172
本文介绍了当多个句柄同时发出信号时,WaitForMultipleObject的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定:我用自动重置事件填充一个句柄数组,并将其传递给等待多个对象,其中bWaitAll = FALSE。



从MSDN:
当bWaitAll为FALSE时,此函数按索引0开始按顺序检查数组中的句柄,直到其中一个对象发出信号。如果多个对象成为信号,该函数返回数组中第一个句柄的索引,该对象被发出信号。



所以现在如果有多个对象发出信号,我会得到第一个的指数。我必须循环通过我的数组看看有没有其他信号?



现在我有一个循环:

  {
WaitForMultipleObjects(...)
如果(未失败)
调用的进程对象。
删除从数组发出信号的句柄。
压缩原子。
}


解决方案


所以,现在如果多个对象信号我会得到第一个的索引。我必须通过我的数组循环
看看有没有其他信号?


为什么不回头这段等待()?如果多个对象发出信号,回来时仍会发出信号。当然,如果在等待对象数组中有一个非常快速的第一个对象,它会使其他人饿死;你所做的是按等待对象数组的顺序排列对象,其频率最低。



BTW,你正在使用一个无尽的),你可以使用goto。如果你真的没有离开循环,无条件的goto最适合地表达你的意图。


Given: I fill up an array of handles with auto reset events and pass it off to WaitForMultipleObjects with bWaitAll = FALSE.

From MSDN: "When bWaitAll is FALSE, this function checks the handles in the array in order starting with index 0, until one of the objects is signaled. If multiple objects become signaled, the function returns the index of the first handle in the array whose object was signaled."

So, now if multiple objects signal I’ll get the index of the first one. Do I have to loop though my array to see if any others have signaled?

Right now I have a loop that’s along the lines of:

For ( ; ; )
{
WaitForMultipleObjects(…)
If  (not failed)
     Process object that called. 
     Remove the handle that signaled from the array.
     Compact the arrary.
}

解决方案

So, now if multiple objects signal I’ll get the index of the first one. Do I have to loop though my array to see if any others have signaled?

Why not just go back round into the Wait()? if multiple objects signalled, they will still be signalled when you come back round. Of course, if you have a very rapidly firing first object in the wait object array, it will starve the others; what you do is order your objects in the wait object array by frequency of firing, with the least frequent being first.

BTW, where you're using an endless for(), you could use a goto. If you really are not leaving a loop, an unconditional goto most properly expresses your intent.

这篇关于当多个句柄同时发出信号时,WaitForMultipleObject的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆