Gracefull thread killing when App ends

Posted on 1998-11-19
Medium Priority
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
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
  • 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.
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

LVL 11

Accepted Solution

mikeblas earned 150 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

752 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