Solved

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

Posted on 2008-10-10
2
321 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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.

757 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

21 Experts available now in Live!

Get 1:1 Help Now