在Visual C ++中重新创建线程

时间:2013-09-03 12:05:50

标签: multithreading mfc

我使用CWinThread m_pThread创建了一个AfxBeginThread()对象。然后我需要消灭它。经过一些操作后,我需要再次创建线程。我再次打电话给AfxBeginThread()

我收到了消息this may be due to corruption of the heap...。我没有在线程中做任何测试。我仍然发现了这个错误。

我通过这些破坏了线程。

    enter code here
     bool bRet = TerminateThread(m_pThread->m_hThread, -1);
  delete m_pThread;     
  m_pThread->m_hThread = NULL;      
  m_pThread = NULL;

1 个答案:

答案 0 :(得分:0)

如果要控制线程的生命周期,请确保关闭m_bAutoDelete,例如

m_pThread = AfxBeginThread( DoWork, NULL, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );
if ( m_pThread ) {
    m_pThread->m_bAutoDelete = FALSE;
    m_pThread->ResumeThread();
}