c++ - WaitForMultipleObjects 会修改*多个*对象的状态吗?

标签 c++ winapi visual-c++ waitformultipleobjects

当使用 WaitForMultipleObjects(... /*bWaitAll=*/FALSE ...) 时该函数显然会修改导致其返回的第一个同步对象的状态。也就是说,如果您有信号自动重置 event ,返回值表明这个事件对象导致函数返回,肯定已经被重置了。

但是,请考虑您有多个对象的情况 - 这里:

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.

因此,您只能返回第一个句柄,并且不知道该索引之后是否有任何事件已发出信号。

对于状态被修改的对象,现在的问题是,当 WaitForMultipleObjects 返回时多个对象已发出信号时,将仅修改第一个对象的状态,还是所有发出信号的对象都会被修改已重置?

文档确实指出:

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.

因此这表明多个对象确实可以修改其状态。然而,这稍微与声明相矛盾:

... this function checks the handles in the array in order starting with index 0, until one of the objects is signaled. ...

此外,这意味着无法将此函数与状态已修改的多个同步对象(例如自动重置事件、信号量等)一起使用,因为您总是会丢失信息。


我在 this answer to "Behavior of WaitForMultipleObjects when multiple handles..." 中找到了一条声明其他人会得出这样的结论(从那里的评论):

WaitForMultipleObjects() scans the handle array from 0 onwards and returns as soon as it finds a signalled handle. Only that first found handle is reset to the unsignalled state; the others are untouched. – user82238 / Mar 25 '09 at 19:27

但想重新询问并可能明确确认这一点。


还有一个interesting discussion over at CodeGuru ,这似乎没有说明这一点。

最佳答案

嗯。瓦达亚知道。

来自Raymond Chen的评论:

If waiting for one event, then only that event is modified. If waiting for all events, then all are modified. That's what the documentation means by "object or objects". Singular if wait-any, plural if wait-all. – Raymond Chen

这将与文档匹配,就像在包含“一个或多个对象”的段落之前一样,在相同的备注子标题下,我们 find :

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.

因此,要回答这个问题,如下所示:如果bWaitAll==FALSE,则第一个对象(报告为返回索引的对象)的状态发生更改。

关于c++ - WaitForMultipleObjects 会修改*多个*对象的状态吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22814435/

相关文章:

c++ - 创建重复对象的 vector ?

c++ - 嵌套 lambda 的效率

c# - 在 C# 中将字符串转换为当前区域性

c++ - 我可以在使用前定义吗

.net - 为什么.NET 4.0 在加载非托管 DLL 时会出现 "CRT not initialized"错误?

visual-c++ - 用于 c++ 联合/结构的 Natvis 可视化工具

c++ - 获取qint64的最高有效字节

java - OpenCV Java,读取中心像素的HSV值

c - 如何使 winapi 中的文件不可执行?

c++ - 使用 WinXp 公共(public)控件时编辑控件背景颜色的奇怪行为