WaitForMultipleObjects( ) and thread object
-
For example: ..... // Create 2 threads and save their handles to hThread1, hThread2 rgHandles[0] = hThread1; rgHandles[1] = hThread2; while (TRUE) { dwIndex = ::WaitForMultipleObjects(2, rgHandles, FALSE, INFINITE); dwIndex -= WAIT_OBJECT_0; ...... // farther process at here } When the thread1 is terminated, the state of thread1 is set to signaled, then WaitForMultipleObjects() returns the index 0 of the array. But when the thread2 is terminated, the WaitForMultipleObjects() still returns the index 0 not index 1. I want WaitForMultipleObjects() to return the correct index, so I can process for this terminated thread. who can help me? Thanks a lot
-
For example: ..... // Create 2 threads and save their handles to hThread1, hThread2 rgHandles[0] = hThread1; rgHandles[1] = hThread2; while (TRUE) { dwIndex = ::WaitForMultipleObjects(2, rgHandles, FALSE, INFINITE); dwIndex -= WAIT_OBJECT_0; ...... // farther process at here } When the thread1 is terminated, the state of thread1 is set to signaled, then WaitForMultipleObjects() returns the index 0 of the array. But when the thread2 is terminated, the WaitForMultipleObjects() still returns the index 0 not index 1. I want WaitForMultipleObjects() to return the correct index, so I can process for this terminated thread. who can help me? Thanks a lot
Jin Yao wrote: But when the thread2 is terminated, the WaitForMultipleObjects() still returns the index 0 not index 1. Are you sure that
thread1
hasn't also terminated at the same time asthread2
? -
Jin Yao wrote: But when the thread2 is terminated, the WaitForMultipleObjects() still returns the index 0 not index 1. Are you sure that
thread1
hasn't also terminated at the same time asthread2
?At the first time, thread1 terminates and WaitForMultipleObjects() returns the WAIT_OBJECT_0 + 0. The state of thread1 object is set to signaled. When the thread2 ends, the thread2 object will also be set to signaled. but at that time, the state of thread1 object is already signaled. So WaitForMultipleObjects() still returns the WAIT_OBJECT_0 + 0 not WAIT_OBJECT_0 + 1. I wish WaitForMultipleObjects() can return the WAIT_OBJECT_0 + 1 when thread2 ends. Thanks
-
At the first time, thread1 terminates and WaitForMultipleObjects() returns the WAIT_OBJECT_0 + 0. The state of thread1 object is set to signaled. When the thread2 ends, the thread2 object will also be set to signaled. but at that time, the state of thread1 object is already signaled. So WaitForMultipleObjects() still returns the WAIT_OBJECT_0 + 0 not WAIT_OBJECT_0 + 1. I wish WaitForMultipleObjects() can return the WAIT_OBJECT_0 + 1 when thread2 ends. Thanks
when a thread in the handle array becomes signalled and the wait satisfied, handle the case and then remove that handle from the array (dec. the count, shift over remaining handles as required..) That handle will remain valid and signalled until you call CloseHandle on it, so you must remove it from the wait.