WaitForMultipleObjects and AutoResetEvents

回答済み WaitForMultipleObjects and AutoResetEvents

  • Tuesday, February 14, 2012 9:05 PM
     
     

    I have a WaitForMultipleObjects that waits for all the AutoResetEvents in a vector. I do want the loop that is waiting on these events to be killed, so I have:




                while(WaitForMultipleObjects(m_hThreadReadyEvents.size(),&m_hThreadDoneEvents[0],true,500))//is this OK?
                {
                    WaitForSingleObject(hIsKillingMutex,INFINITE);
                    if (isKilling)
                        break;
                }


    The question I have is, if the WaitForMultipleObjects times out because only some of the events have fired, will the events that have fired get Reset? (I'm hoping not....I don't want to make them all manual reset events!)

    • Moved by Rahul V. Patil Wednesday, May 23, 2012 8:22 PM API in Windows SDK (From:Parallel Computing in C++ and Native Code)
    •  

All Replies

  • Wednesday, May 23, 2012 8:38 PM
     
     Answered

    Hi Idea Hat,

    Generically speaking WaitForMultipleObjects (or any wait function) does not consume any signal state from an object it is not reporting.

    So, in your specific case, none of the events will be reset if the function returns timeout.

    Cheers,
    Pedro


    PedroT

    • Marked As Answer by Idea Hat Thursday, May 24, 2012 3:02 PM
    •  
  • Thursday, May 24, 2012 10:27 AM
     
     Answered

    Well, from the function documentation of WaitForMultipleObjects.

    When bWaitAll is TRUE, the function's wait operation is completed only when the states of all objects have been set to signaled. The function does not modify the states of the specified objects until the states of all objects have been set to signaled. For example, a mutex can be signaled, but the thread does not get ownership until the states of the other objects are also set to signaled. In the meantime, some other thread may get ownership of the mutex, thereby setting its state to nonsignaled.

    Emphasis mine. So this to me states that any auto reset events are only reset after all events are signaled.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

    • Marked As Answer by Idea Hat Thursday, May 24, 2012 3:02 PM
    •