A question about WaitForMultipleObjects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abubakar

    A question about WaitForMultipleObjects

    Hi all,
    I'm using WaitForMultiple Objects, 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 WaitForMultiple Objects with array of
    thread handles. So now my question is that when WaitForMultiple Objects
    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 WaitForMultiple Objects can be
    performed on the array.

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

    Regards,

    ...ab


  • SvenC

    #2
    Re: A question about WaitForMultiple Objects

    Hi Abubakar,
    Hi all,
    I'm using WaitForMultiple Objects, 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.

    --
    SvenC

    Comment

    • Doug Harrison [MVP]

      #3
      Re: A question about WaitForMultiple Objects

      On Tue, 5 Aug 2008 13:11:06 +0500, "Abubakar" <q@y.comwrote :
      >Hi all,
      >I'm using WaitForMultiple Objects, 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 WaitForMultiple Objects with array of
      >thread handles. So now my question is that when WaitForMultiple Objects
      >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 WaitForMultiple Objects 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.

      --
      Doug Harrison
      Visual C++ MVP

      Comment

      • Abubakar

        #4
        Re: A question about WaitForMultiple Objects

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

        "SvenC" <SvenC@nospam.n ospamwrote in message
        news:92969466-6750-421F-AABC-29E87E98D35B@mi crosoft.com...
        Hi Abubakar,
        >
        >Hi all,
        >I'm using WaitForMultiple Objects, 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.
        >
        --
        SvenC

        Comment

        Working...