【发布时间】:2011-08-02 12:43:23
【问题描述】:
我运行 openCL /openGL 程序,它使用 wxWidget 作为 gui 环境
在派生自 wxThread 的类的对象内部,我执行了一些复杂的计算并构建了许多 openCL 程序。 我想删除线程。但是线程并没有立即删除——它会继续构建程序,并且在完成所有编译之后。
我知道我可以使用wxThread::KIll() 退出线程,但它会导致一些内存问题,所以它不是一个真正的选择。
我有从 wxFrame 派生的 myFrame 类。它有 pCanvas 指针,它指向从 wxCanvas 派生的对象 *pCanvas 对象包括 myThread(运行复杂的计算)
void myFrame::onExit(wxCommandEvent& WXUNUSED(event))
{
if(_pCanvas != NULL )
{
wxCriticalSectionLocker enter(_smokeThreadCS);
// smoke thread still exists
if (_pCanvas->getThread() != NULL)
{
//_pCanvas->getSmokeThread()->Delete(); <-waits until thread ends and after it application terminates
_pCanvas->getSmokeThread()->Kill(); <- immediately makes the application not responding
}
}
// exit from the critical section to give the thread
// the possibility to enter its destructor
// (which is guarded with m_pThreadCS critical section!)
while (true)
{
{ // was the ~MyThread() function executed?
wxCriticalSectionLocker enter(_smokeThreadCS);
if (!_pCanvas->getSmokeThread()) break;
}
// wait for thread completion
wxThread::This()->Sleep(1);
}
DestroyChildren();
Destroy();
// Close the main frame, this ends the application run:
Close(true);
}
【问题讨论】:
-
IIUC 线程获取临界区并进行杀戮不是线程被杀吗?
-
杀死这样的线程并不是一个好主意。
标签: c++ multithreading visual-studio-2010 wxwidgets