WaitForMultipleObjects: How to asynchronously wait?
Hello!
I'm using WaitForMultipleObjects to wait for three threads to finish.
CWinThread* thread1 = AfxBeginThread(foo,NULL,THREAD_PRIORITY_NORMAL,
0,0,NULL);
CWinThread* thread2 = AfxBeginThread(bar,NULL,THREAD_PRIORITY_NORMAL,
0,0,NULL);
CWinThread* thread3 =
AfxBeginThread(foobar,NULL,THREAD_PRIORITY_NORMAL,0,0,NULL);
HANDLE h[] = {thread1->m_hThread,thread2->m_hThread,thread3-
m_hThread};
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.
What am I doing wrong? Do I have to remove the handle for the thread
which is finished from the HANDLE array before calling
WaitForMultipleObjects again? How can I do that?
Many thanks,
MIchael