-
November 15th, 2003, 09:36 AM
#1
thread exit & memory
I have a UI thread(CMyTHread) derived from CWinThread.
CMyThread* m_pThread; //member of CTestDialog;
CTestDialog::OnNewJob()
{
...
if( NULL != m_pThread )
{
m_pThread = new CMyThread( ...some params... );
m_pThread->CreateThread();
}
else
{
//it is already doing a job. it is not finished.
}
...
}
//thread has finished its job
//and mimic us about that by posting a win_msg
// then went out 'IniInstance' method
//so we are here
CTestDialog::OnThreadFinished()
{
delete m_pThread;
m_pThread = NULL;
}
My question is that:
Does 'delete m_pThread;' exit the thread and clear the whole memory successfully?
I ask this question because I noticed that
CMyThread::ExitInstance() method never called after delete line.
(CMyThread destructor is virtual)
*Please note that; I never called AfxEndThread() and PostQuitMessage() in the boundary of CMyThread class!
-
November 16th, 2003, 08:09 PM
#2
Yes, you are right that the thread doesn't exit when you delete m_pThread. The proper way of terminating your thread is to quit from the loop where your thread is running, i.e. CMyThread::Run().
An easy way is to check a killed flag while looping. This flag can be set via member function.
Code:
CMyThread::Run()
{
while( !m_bKill )
{
// Perform your task
...
}
}
CMyThread::Kill()
{
m_bKill = TRUE;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|