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

Posted on 2005-04-18
Medium Priority
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

Accepted Solution

rcarlan earned 252 total points
ID: 13813107
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

itsmeandnobodyelse earned 248 total points
ID: 13813245
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

ID: 15656763
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

850 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