Re: WaitForMultipleObjects: How to asynchronously wait?
"Daneel" <michael.ransburg@gmail.com> wrote in message
news:1171204471.647504.281150@s48g2000cws.googlegroups.com
I want to wait for all of them. However, as some thread might take
longer than the others, I want to get notified immediately if one of
the threads finishes in order to update the GUI:
for (i = 0; i < 3; i++) {
WaitForMultipleObjects(3, h, FALSE, INFINITE);
// do something
UpdateData(FALSE);
}
The problem is that as soon as one of the threads is finished, the for
loop executes all three times without WaitForMultipleObjects waiting
for the remaining 2 threads.
Once a thread handle is signalled (the thread has terminated) it remains
signalled until it's closed. That's why WaitForMultipleObjects returns
immediately when you pass this hande to it again. Wait is not
edge-triggered (triggered by transition from non-signalled to signalled
state), it's level-triggered (triggered just by the fact that a handle
is signalled, no matter how it got there).
Return value of WaitForMultipleObjects tells you which handle caused it
to return. You now need to prepare a new HANDLE[] array containing only
the two remaining handles, and call WaitForMultipleObjects again with
just these two handles.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925