CloseHandle(CreateThread(...));

I was browsing through some code when I came across a line of code which said:

CloseHandle( CreateThread(NULL, 0, PeculiarThreadProc, NULL, 0, NULL) );

And I thought, wait a minute, is he closing the handle to the thread immediately after its created. I pinged my TL and he said the thread will continue to run until it terminates. I thought I had read in documents that a thread will terminate when the last handle to it was closed and here it was the last and only handle to the thread. A closer look at the documentation made it clear,

"The thread object remains in the system until the thread has terminated and all handles to it are closed through a call to Closehandle."

I had missed the 'and'. Anyways, to kill my curiosity for good I went ahead and tested the following code,

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
    CloseHandle( CreateThread(NULL, 0, PeculiarThreadProc, NULL, 0, NULL));

     while(1)
     {
          Sleep(1000);
     }

    return 0;
}

DWORD WINAPI PeculiarThreadProc(void *lPtr)
{
    DWORD tick;
    while(1)
    {
        tick = GetTickCount();
        printf("Tick count:%u\r\n", tick);
        Sleep(1000);       
    }
}

And as expected the thread kept running in all its glory.

Suppose we modify our program a little as below,

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
    CreateThread(NULL, 0, PeculiarThreadProc, NULL, 0, NULL);
    
     while(1)
     {
          //do nothing
          Sleep(1000);
     }

    return 0;
}

DWORD WINAPI PeculiarThreadProc(void *lPtr)
{
        printf("PeculiarThreadProc");
        Sleep(100);       
}

So in this case, the thread terminates but the handle to the thread is not closed. So does that mean that the thread object remains in the system? The answer is yes, and the thread object will exist in signaled state. And of course, if the main thread itself exits then all hope is lost (;

This article is part of the GWB Archives. Original Author: TechTwaddle

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.