Check if thread is alive in Windows

0

I recently tried to write a code to check if a thread in Windows is alive or not. I searched this forum and found discussions like: How to check if a process or thread is alive or not given their IDs in C++?.

I understand I can use OpenThread API. However it doesn't seem to work in my code as follows.

DWORD WINAPI myThread( LPVOID lpParam )
{
    cout<<"child thread"<<endl;
    return 0;
}

int main(void)
{
    DWORD  lTldID = 0;
    HANDLE lTldHD = CreateThread(NULL, 0, myThread, 0, 0, &lTldID);

    WaitForSingleObject(lTldHD, INFINITE);
    HANDLE lHD = OpenThread(0x0040, FALSE, lTldID);
    return 1;
}

I expect that the HANDLE lHD should be NULL since the thread 'myThread' should have finished at the time of calling OpenThread(). However, I always got NOT NULL values like 0x00000068. I don't know why this happened. Any idea?

Thanks, Xiaomo

windows
multithreading
asked on Stack Overflow Dec 11, 2012 by Xiaomo Liu • edited May 23, 2017 by Community

1 Answer

0
BOOL WINAPI GetExitCodeThread(
  _In_  HANDLE  hThread,
  _Out_ LPDWORD lpExitCode
);

This function returns immediately. If the specified thread has not terminated and the function succeeds, the status returned is STILL_ACTIVE.

answered on Stack Overflow Aug 17, 2017 by shaoheng • edited Aug 17, 2017 by Werner Henze

User contributions licensed under CC BY-SA 3.0