A question about WaitForMultipleObjects

S

SvenC

Hi Abubakar,
Hi all,
I'm using WaitForMultipleObjects, I give an array of handles and give
it FALSE in bWaitAll param. What I want to do is, I spawn few threads
upto N, than I wait for any of them to end, when any one or more of
them ends, I want to spawn as many threads as were ended.

Why do you want to start/end/restart threads?
This sounds like a bad design.
Start threads to act as a thread pool that lives as long as your process
needs the threads to do their work.
A typical design pattern is that the thread function is waiting in a loop
on some object to be signalled. When signalled it might read some
arguments from a task queue to process that task. One task or one
signal would be an indicator that the process need to shut down and
the thread should leave its loop.
An efficient signal/queue technique are windows IO completion ports.
Use google or msdn to get the details.
 
A

Abubakar

Hi all,
I'm using WaitForMultipleObjects, I give an array of handles and give it
FALSE in bWaitAll param. What I want to do is, I spawn few threads upto N,
than I wait for any of them to end, when any one or more of them ends, I
want to spawn as many threads as were ended. So Its going to be a while
loop, inside that is going to be a WaitForMultipleObjects with array of
thread handles. So now my question is that when WaitForMultipleObjects
returns, how do I know which handle was the one that got signaled and how do
I fit in the handle(s) so that another WaitForMultipleObjects can be
performed on the array.

Using visual c++ 2k5 on windows xp sp2 (all native c++ code);

Regards,

...ab
 
D

Doug Harrison [MVP]

Hi all,
I'm using WaitForMultipleObjects, I give an array of handles and give it
FALSE in bWaitAll param. What I want to do is, I spawn few threads upto N,
than I wait for any of them to end, when any one or more of them ends, I
want to spawn as many threads as were ended. So Its going to be a while
loop, inside that is going to be a WaitForMultipleObjects with array of
thread handles. So now my question is that when WaitForMultipleObjects
returns, how do I know which handle was the one that got signaled and how do
I fit in the handle(s) so that another WaitForMultipleObjects can be
performed on the array.

The thread pool suggestion may be the right approach, but to answer your
questions, a successful WFMO returns WAIT_OBJECT_0+N, where N indexes the
handle array. More handles may be signaled than just that one, but I
wouldn't try to detect this, as your WFMO loop will handle it fine, and you
can't beat the race condition inherent in trying to detect this with (say)
WFSO and zero timeout. If you have a new handle to wait on, you can simply
replace the old one in your handle array.
 
A

Abubakar

Hmm, just made a thread pool thing. Noticed it was easier than the way I
wanted to go. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top