Gracefull thread killing when App ends

Posted on 1998-11-19
Last Modified: 2013-11-20
My app will sometimes create a CWinThread-derived thread to go do some work.  It is technically a UI thread, since it has a window, used to monitor what is going on, but it does not get any user input.

I have the thread set up with the m_bAutoDelete to TRUE, so that once the tread is finished it deletes itself.  This works quite well until I try quitting the program while the thread is still running.  When this happens the thread gets killed, and memory leaks abound.

So I thought, when the app is quitting, I could somehow inform the thread that it is time to die.  However, because the thread can destroy itself, I don't know if I have a thread still running or not.  The thread destroys itself, but my pointer still has a value, alas one that is useless.

How can I figure out if the thread is still running when I quit, and if so tell it to quit before the app finishes?
Question by:carlosn
  • 3
  • 2

Author Comment

ID: 1325008
Edited text of question

Expert Comment

ID: 1325009
When you quit the application all threads of this will be automaticaly killed. Now your problem with memory leaks can be easily solved by only puting sleep(NNNN) in your WM_ONCLOSE member function of CFrmWnd Class. NNNN denotes milliseconds sleep(2000). That depends on your user interface thread. You try it out with changing the values of NNN and see in debug mode whether  your user interface thread is giving any memory leak or not. Please keep updating the interaction.

Author Comment

ID: 1325010
I thought about doing this, but I would like some option that is a bit more graceful.  I'd rather not have an absolute wait, especially since most of the time the thread is already dead anyway.  The issue is only when the user happens to kill the app during the brief time the thread is running.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 11

Accepted Solution

mikeblas earned 50 total points
ID: 1325011
There's no silver bullet here.

You have to code your threads to watch for an event or look for a flag. When they see that signal, they should exit.

When you want to exit, set that flag and then wait for the threads to stop executing. You can wait for a thread by calling WaitForSingleObject() (or any of its friends) on the handle of the thread.

Don't ever, under any circumstances, call the TerminateThread() API.

B ekiM


Author Comment

ID: 1325012
I see what you are getting at here.  I hadn't thought of that approach.  My only concern, if the thread deletes itself after completion, there isn't a handle to wait on any more.  Say I had a thread pointer 'pThread'

The thread executed, and then deleted itself because of the m_bAutoDelete = TRUE.  What happens if I try a WaitOnSingleEvent(pThread->m_hThread);  It seems that there is a slim chance that whatever happens to lie in the memory once occupied by the variable could point to a waitable object, though I guess that is fairly slim.

LVL 11

Expert Comment

ID: 1325013
In actuality, there is.  The thread handle is valid until someone calls CloseHandle() on it.

The CWinThread object, on the other hand, can delete itself as you describe. And, for that reason, m_bAutoDelete is of dubious value in the class.

B ekiM

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
method notes when mouse over in eclipse 5 87
lucky13 challenge 11 153
withoutTen challenge 14 130
Unix Command -- Challenging  question 7 95
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

791 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