WaitForMultipleObjects等待多个事件的时候,如果同时有多于一个事件被触发,该如何处理所有事件呢?SDK里面讲,如果在WaitForMultipleObjects调用期间,有不止一个事件被触发,则该函数的返回值为触发的多个事件中索引值最小的那个事件的索引,这个索引减掉WAIT_OBJECT_0即可得到该事件的真正索引,从而处理这个事件。
但是一起触发的其他事件如何处理呢?有没有被丢弃呢?会不会丢失事件啊?
但是一起触发的其他事件如何处理呢?有没有被丢弃呢?会不会丢失事件啊?
DWORD nCount,
const HANDLE* lpHandles,
BOOL bWaitAll,
DWORD dwMilliseconds
);bWaitAll
[in] If this parameter is 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.bWaitAll对指定的这nCount个内核对象的两种等待方式进行了指定,为TRUE时当所有对象都被通知时函数才会返回,为FALSE则只要其中任何一个得到通知就可以返回。
If WaitForMultipleObjects were not all-or-nothing in this way, you would probably end up with deadlocks. Be careful when you use wait functions.
dwHandles[0] = XXX1;
dwHandles[1] = XXX2;
bWaitStatus = WaitForMultipleObjects(2, dwHandles, FALSE,INFINITE);
switch (bWaitStatus)
{
case WAIT_OBJECT_0:
// do something...
break;
case WAIT_OBJECT_0 + 1:
// do something...
break;
default:
break;
}
英文俺还看得懂好不好,看清题目再回答吧,摘一段Platform SDK有什么意思呢,大家都会看!
建议你再看看这段:
“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 signaled during the call, this is the array index of the signaled object with the smallest index value of all the signaled objects.”
To jiangsheng(蒋晟.Net[MVP]):
MVP就是MVP,一句话点中要害,"use WiatForSingleObject with timeout set to 0 to check the state of an object",看来如果多个handle一起触发,处理起来真的好麻烦啊,要从返回的索引开始一个一个的向后测试才能知道到底有那几个handle触发。To Atomictry(天影):
你这代码能处理2个handle一起触发吗?狂汗 !
也就是基本不用考虑多个事件同时触发的情形吗?
应该不会丢,应该是使用ResetEvent信号才会重值
Handles[0] = CreateEvent(NULL, FALSE, TRUE, NULL);
Handles[1] = CreateEvent(NULL, FALSE, TRUE, NULL);
WaitForMultipleObjects(2, Handles, FALSE,INFINITE);//如果这里没有死锁就说明没有丢失啦
WaitForMultipleObjects(2, Handles, FALSE,INFINITE);
当然在单处理器系统里,多个事件一起触发几乎是不可能的,但是smp系统呢?超线程系统呢?我现在的机器就是超线程的,是不是该考虑这个问题呢???To lsgt():
你说的很有道理!
{
HANDLE Handles[2];
Handles[0] = CreateEvent(NULL, FALSE, TRUE, NULL);
Handles[1] = CreateEvent(NULL, FALSE, TRUE, NULL); printf("开始第一次等待...");
printf("完成[%d]。\n开始第二次等待...", WaitForMultipleObjects(2, Handles, FALSE,INFINITE)-WAIT_OBJECT_0); //如果这里没有死锁就说明没有丢失啦
printf("完成[%d]。\n开始第三次等待...", WaitForMultipleObjects(2, Handles, FALSE,INFINITE)-WAIT_OBJECT_0); WaitForMultipleObjects(2, Handles, FALSE,INFINITE);
printf("完成。");
}编译后输出:
开始第一次等待...完成[0]。
开始第二次等待...完成[1]。
开始第三次等待...
Handles[0] = CreateEvent(NULL, FALSE, TRUE, NULL);
Handles[1] = CreateEvent(NULL, FALSE, TRUE, NULL);
while( TRUE )
{
switch(::WaitForMultipleObjects(2, Handles, FALSE,INFINITE))
{
case WAIT_OBJECT_0:
// do some thing...
break;
case WAIT_OBJECT_1:
// do some thing...
break;
...
}
}
HANDLE Handles[2];
Handles[0] = CreateEvent(NULL, FALSE, TRUE, NULL);
Handles[1] = CreateEvent(NULL, FALSE, TRUE, NULL);
WaitForMultipleObjects(2, Handles, FALSE,INFINITE);
int flag = 0;
for(int i=0; i<2; i++)
{
if(waitforsingleobject(Handle[i], 0)!=WAIT_TIMEOUT)
flag |= 1<<i;
}
//然后根据flag的值处理:1为Handle[0]有singal,2为Handle[1]有singal,3为两个都有