Orphaned handles of type "Thread" keeps on growing in my Win32 C++ Application?

Posted on 2005-04-18
Last Modified: 2013-12-03

    I have multithreaded C++ Application that runs both on Windows as well as Solaris platforms. At any point of time the application is running with 26 persistent threads. There are some worker threads also but they do their job and finished.

My problem is that in the application the number of handles of type "Thread" are continously increasing as time goes by. When I see the task manager or when I see the stack dump of all the threads in my application then it matches with the number of threads it is showing in the task manager. But when I dump the handles then the number of thread type handles are much greater then actual number of threads in the application.

Please let me know why these thread handles are growing continously when I am using CloseHandle() to terminate the thread handles?

Also these handles are not in proportion with the Worker Threads that are spawning and finished every x seconds. Implies that the handles of temorary threads are getting closed.

Question by:Abhi072299
    LVL 9

    Accepted Solution

    CloseHandle will not terminate the thread. It will only "disconnect" your program from it (i.e. you won't be able to refer to it, but the thread will continue to run happily). Basically, a thread will continue to run until either *it* decides to stop (e.g. return from the associated procedure/function) or it is forcefully terminated (e.g. by a call to TerminateThread or by its parent process shutting down).

    LVL 39

    Assisted Solution

    You should design your worker threads that they can be stopped by the main thread:

    struct ThreadInfo
        Parent* pParent;
        Data*   pData;
        bool      goon;

    void threadFunc(void* pParam)
          ThreadInfo* pInfo = (ThreadInfo*)pParam;
          pInfo->goon = true;

          // thread loop
          while (true)
                 // check if goon flag has turned to false in the meantime
                 if (!goon)
                 // sleep a little while

    With that the main thread and/or the thread itself could smoothfully terminate. However, it needs to check the goon flag within short time intervals.

    Regards, Alex
    LVL 14

    Expert Comment

    No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
    I will leave the following recommendation for this question in the Cleanup topic area:

    Split between rcarlan, itsmeandnobodyelse

    Any objections should be posted here in the next 4 days. After that time, the question will be closed.

    EE Cleanup Volunteer

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now