Thread: WaitForMultipleObjects Trickiness

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Question WaitForMultipleObjects Trickiness

    Hello,

    I'm trying my hand at coming up with an event-driven multithreading framework, based upon the idea of each thread waiting on a variable number of event handles. When any given event is signaled, the thread examines a queue associated with the event, does something related to what it finds there, then goes back to waiting.
    Code:
    while ((dwRet = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE)) != WAIT_FAILED)
    {
        // do stuff
    }
    Now for the quirk: when waiting for any event to be signaled (not all of them, otherwise the FALSE in the above statement would be TRUE), each event is evaluated in order. So as it stands, if the first event in the hEvents array was particularly noisy, the thread would never see signals from the other events.

    The way to get around this is to change the order of the events each time one is signaled, so that the signaled one is pushed to the end of the array. This would give other events a chance.

    So with this in mind, I devised the following function, to be called at the end of the loop:-
    Code:
    void ReorderWaitHandles(DWORD nCount, LPHANDLE lpHandles, DWORD nSignaledIndex, void **ppAssociated)
    {
        HANDLE hTemp;
        void *pTemp;
    
        if (nSignaledIndex < nCount - 1)
        {
            hTemp = lpHandles[nSignaledIndex];
            memcpy(&lpHandles[nSignaledIndex], &lpHandles[nSignaledIndex + 1], ((nCount - 1) - nSignaledIndex) * sizeof(*lpHandles));
            lpHandles[nCount - 1] = hTemp;
            if (ppAssociated)
            {
                pTemp = ppAssociated[nSignaledIndex];
                memcpy(&ppAssociated[nSignaledIndex], &ppAssociated[nSignaledIndex + 1], ((nCount - 1) - nSignaledIndex) * sizeof(*ppAssociated));
                ppAssociated[nCount - 1] = pTemp;
            }
    
        }
    
    }
    The function takes an array lpHandles, of nCount size, pushes lpHandles[nSignaledIndex] to the end of the array and optionally does the same for an identically-ordered pointer-sized array (the queues, in this case).

    However, I seem to find that when I put this into practice, with an array of four event handles:-
    [Event 1]
    [Event 2]
    [Event 3]
    [Event 4]

    If I opt to remove e.g. Event 2 from the array, with the following code:-
    Code:
    hEvents[1] = NULL;
    nCount--;
    After ReorderWaitHandles is called, WaitForMultipleObjects fails. It does this because of the presence of a NULL handle in the hEvents array. But I can't seem to work out how to resolve this. I know it's something simple, but my mind is clouded. Help please?

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Swap the one to remove with the last one, and decrement the count. Or if you want to preserve the order of the elements, shuffle the remaining elements down a position, and decrement the count.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    instead of null - put there the handle of the current thread... till it exits it will never signaled
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    I did eventually sup enough caffeine to re-work the ReorderWaitHandles function to provide the option of "deleting" the signaled element of the arrays, just performing the memcpy()s without saving the element. Worked a charm. Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terrible lag in WaitForMultipleObjects
    By Verdagon in forum Windows Programming
    Replies: 5
    Last Post: 12-28-2011, 11:28 AM
  2. WaitForMultipleObjects and listen()/accept()
    By Verdagon in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2011, 10:22 PM
  3. WaitForMultipleObjects for Boost.Thread?
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 12:18 PM
  4. Trickiness while trying to implement an automounter ...
    By sirlark in forum Linux Programming
    Replies: 1
    Last Post: 10-14-2007, 01:19 PM