Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-10
2
Medium Priority
?
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

721 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