Solved

Parent thread terminates before child threads are complete.  Should handles leak?

Posted on 2008-10-10
2
322 Views
Last Modified: 2009-10-09
I have a very tricky threading issue and I think I've narrowed it down:

My program creates a thread to query a database for information.  If the database contains records that match, separate threads are spawned to generate email alerts for the records.

This seems to work well in normal operating environments (alert records create the alert emails).

When the mailserver is overloaded, slow, or shut down, the child threads do not complete in a timely fashion (waiting on mailserver).  The parent database query thread completes and terminates, but its child threads are still waiting for a (potentially non-existant) mailserver.

The child threads eventually complete (either successfully or gracefully fail), but handles are leaked.

I have tested this code with a shut-down mailserver, and handles leak anytime the parent thread completes before the child threads error out.

Handles do NOT leak if I remove the threading and attempt to send the emails non-multithreaded (they fail one at a time).

Is this type of threading/handle leaks expected when a parent terminates before a child?
0
Comment
Question by:ttitus
2 Comments
 
LVL 17

Accepted Solution

by:
CSecurity earned 500 total points
ID: 22691116
When you create threads you need to wait for them to terminate with

WaitForMultipleObjects(HandleArray, INFINITE)

It will wait for all handles in handle array to finish their job... So you need to have this code in parent (creator of threads).

When threads started working you need to define a timeout in threads, when timeout passed you need to close your thread and return from thread function, when all threads finish their job, now you can close all handles to create or associate them to new threads, you can do them with

for (int i=0;i < THREADCOUNT; i++)
CloseHandle(ThreadArray[i]);

0
 

Author Comment

by:ttitus
ID: 22691173
Right now, when I create a new thread, I immediately close the handle to the thread as I don't need it and don't want it to leak:

_pbeginthreadex(......ThreadHandle..);
CloseHandle(ThreadHandle);

This seems to work well for all of my current threads, but does it create problems when there's multiple hierarchies of threads?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

939 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

8 Experts available now in Live!

Get 1:1 Help Now