我用MFC做多线程,先用AfxBeginThread建立了个新线程CWinThread *thread1,
thread1 = AfxBeginThread((AFX_THREADPROC)downloadData,this,0,0,CREATE_SUSPENDED);
在这个线程内部用AfxEndThread(0,true)结束进程,然后在线程外查看thread1并不为NULL。
AfxEndThread第二个参数不是"Deletes the thread object from memory"的意思吗?thread1不为NULL,是不是还占用着一些资源?
thread1 = AfxBeginThread((AFX_THREADPROC)downloadData,this,0,0,CREATE_SUSPENDED);
在这个线程内部用AfxEndThread(0,true)结束进程,然后在线程外查看thread1并不为NULL。
AfxEndThread第二个参数不是"Deletes the thread object from memory"的意思吗?thread1不为NULL,是不是还占用着一些资源?
pThread->m_bAutoDelete = FALSE;
pThread->ResumeThread();
...你需要手动删除
CloseHandle(pThread->m_hThread);
delete pThread;
pThread = NULL;
{
#ifndef _MT
nExitCode;
bDelete;
#else
// remove current CWinThread object from memory
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
CWinThread* pThread = pState->m_pCurrentWinThread;
if (pThread != NULL)
{
ASSERT_VALID(pThread);
ASSERT(pThread != AfxGetApp()); // cleanup OLE if required
if (pThread->m_lpfnOleTermOrFreeLib != NULL)
(*pThread->m_lpfnOleTermOrFreeLib)(TRUE, FALSE); if (bDelete)
pThread->Delete();
pState->m_pCurrentWinThread = NULL;
} // allow cleanup of any thread local objects
AfxTermThread(); // allow C-runtime to cleanup, and exit the thread
_endthreadex(nExitCode);
#endif //!_MT
}
void CWinThread::Delete()
{
// delete thread if it is auto-deleting
if (m_bAutoDelete)
delete this;
}确实是delete this,但删除后this并不是NULL,只是他指向的内存无效了
根据源码来看,只有m_bAutoDelete和AfxEndThread的第二个参数bDelete同时为true才自动删除线程。