Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-10
2
Medium Priority
?
329 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:Tim Titus
2 Comments
 
LVL 17

Accepted Solution

by:
CSecurity earned 2000 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:Tim Titus
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 clear a vector as well as how to detect empty vectors in C++.
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.

783 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