Search code examples
multithreadingrecursionwaitwaitforsingleobjectwaitformultipleobjects

can Multiple threads depend on a single manual Kill-event?


I have a MFC code with multiple threads that all make recursive calls to a subroutine, with different parameters. In the beginning of the subroutine, I make a call to function CheckKillEvent():

bool CTestShellDlg::CheckKillEvent()
{
    DWORD waitS;
    waitS = WaitForSingleObject(h_KillEvent, 0);
    switch (waitS)
    {
    case WAIT_OBJECT_0:
        return true;
        break;
    case WAIT_TIMEOUT:
        return false;
        break;
    default:
        IERROR
            break;
    }
}

and return() immediately if CheckKillEvent returns true.

fyi, h_killEvent is initialized as:

h_KillEvent = CreateEvent(NULL, true, false, NULL);

ie, it has a manual reset.

However, these threads seem to take (literally) forever to finish after I set the Kill-event as below:

bool CTestShellDlg::KillThreads()
{
        //Signall the killing event
        SetEvent(h_KillEvent);
        if (WaitForMultipleObjects(,,true,)==...)
        {
            ResetEvent(h_KillEvent);
            return true; //Killing successful
        }
    else
        return false; //Killing failed
}

The question is, is there an issue with calling CheckKillEvent() from multiple threads? Should the WaitForSingleObject() be done inside a critical section or something? Or is it simply my recursive code being bad at recursing back to a point where it no longer calls itself?


Solution

  • As Hans suggested in the comment, the problem was in fact with the message pump being blocked. Always best to assign separate threads for tasks that might take long or might themselves need access to the message pump.