Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WaitForMultipleObjects

Status
Not open for further replies.

barryp

Programmer
Jan 30, 2002
48
GB
I have N (atually 35) processes to complete - all just number crunching. Since I have a dual core PC I thought of using threads. So I kick off 2 threads & call WaitForMultipleObjects(2,h,FALSE,INFINITE) & pick up the rsponse, determine which thread has completed, update the paras & kick it off again. Fine - but sometimes one thread seems to complete & not get kicked off again. I end up with just one processor running. Could it be that 2 threads finish together? How can I tell from WaitForMultipleObjects is both threads have completed ????
 
MSDN said:
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.
It would seem to me that losing an event is entirely possible.

One suggestion would be to wait for each thread individually, with a small timeout (say half a second), so that in effect you poll each thread in turn to see if it has finished.

Another approach would be to make the worker threads send a message to your controlling thread when they're done. Messages will queue properly even when both threads exit "at the same time".


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Salem
Thanks. I've just improved things by adding a sleep(1000) between the first two beginthreads(). Now its working. But it would be good to fix it cleanly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top