WaitForMultipleObjects 会修改*多个*对象的状态吗? [英] Will WaitForMultipleObjects modify the state of *multiple* objects?

查看:27
本文介绍了WaitForMultipleObjects 会修改*多个*对象的状态吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When using WaitForMultipleObjects(... /*bWaitAll=*/FALSE ...) the function will obviously modify the state of the first synchronization object that causes it to return. That is, if you have (had) a signaled auto-reset event, and the return value indicates that this event object caused the function to return, that surely it has been reset.

但是,考虑一下您有多个对象的情况——这里:

However, consider the case where you have multiple objects - here:

bWaitAllFALSE时,该函数检查数组中的句柄按从索引 0 开始的顺序,直到对象之一发出信号.如果多个对象发出信号,该函数返回对象已发出信号的数组中的第一个句柄.

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 you only get back the first handle, and you do not know if any events after this index have been signaled.

对于状态被修改的对象,现在的问题是,如果在WaitForMultipleObjects返回时已经有多个对象发出信号,是否只会修改第一个的状态,或者所有发出信号的对象都会被修改重置了吗?

For objects whose state is modified, the question now is, iff multiple objects had been signaled at the time WaitForMultipleObjects returned, will only the state of the first one be modified, or will all signalled objects have been reset?

文档说明:

该函数修改某些类型同步的状态对象.修改只发生在对象或对象信号状态导致函数返回.

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.

所以这表明确实可以修改多个对象的状态.然而,这略微与声明相矛盾:

so this would indicate that it is indeed possible for multiple objects to have their state modified. However, this slightly contradicts the statement:

... 这个函数检查数组中的句柄按从索引 0 开始的顺序,直到对象之一发出信号....

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

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

And furthermore it would mean that it is impossible to use this function with multiple synchronization objects (like auto reset event, semaphores, etc.) that have their state modified, as you'll always loose information.

我在此答案中找到了多个句柄时WaitForMultipleObjects的行为..."的声明,其他人会得出结论(从那里的评论):

I have found a statement in this answer to "Behavior of WaitForMultipleObjects when multiple handles..." that others would conclude that (from comment there):

WaitForMultipleObjects() 从 0 开始扫描句柄数组,然后一旦找到一个有信号的句柄就返回.只有第一个发现句柄重置为无信号状态;其他人未受影响.——user82238/2009 年 3 月 25 日 19:27

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

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

but would like to re-ask and possobly confirm this explicitly.

还有一个 在 CodeGuru 上进行了有趣的讨论,这似乎没有说明这一点.

There is also an interesting discussion over at CodeGuru, that doesn't seem to shed any light on this.

推荐答案

好吧.不知道.

来自Raymond Chen的评论:

如果等待一个事件,则仅修改该事件.如果等待所有事件,然后所有事件都被修改.那是什么文档是指一个或多个对象".单数如果等待任何,复数如果全部等待.– 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

这将匹配文档,因为在包含 object or objects" 的段落之前,在相同的 Remarks 子标题下,我们查找:

This would match the documentation, as prior to the paragraph containing "object or objects", under the same Remarks sub-heading, we find:

当bWaitAll为TRUE时,函数的等待操作才完成当所有对象的状态都设置为有信号时.函数不修改指定对象的状态,直到状态已将所有对象的 设置为已发出信号.

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 那么只有第一个对象(报告为返回的索引的那个)改变了它的状态.

So to answer the question, it follows: If bWaitAll==FALSE then only the first object (the one reported be the returned index) has its state changed.

这篇关于WaitForMultipleObjects 会修改*多个*对象的状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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