Solved

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

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

Technology Partners: 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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

696 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