wxwidgets - 以正确的方式退出线程(wxwidgets - exit the thread the right way)

我运行使用wxWidget作为GUI环境的openCL / openGL程序

类的内部对象,派生自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); }

I run openCL /openGL program which uses wxWidget as gui enviroment

Inside object of class ,which derives from wxThread,I perform some complicated calculations and build many openCL programs. I want to delete the thread .But the thread is not deleted immediately – it continue to build programs and just after it finishes with all the compilations.

I know that I can use wxThread::KIll() to exit the thread but it cause some memory problems so its not really an option.

I have myFrame class which is derived from wxFrame.it has pCanvas pointer ,which points to the object which is derived from wxCanvas *pCanvas object includes the myThread (which runs the complicated calculation)

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); }

最满意答案

杀死这样的线程确实非常糟糕。 最好给线程一个清理的机会。

通常通过定期检查一个标志来告诉它退出:

volatile bool continue_processing = true; thread thread; void compile_thread() { while(continue_processing) { // compile one OpenCL program. } } void terminate() { read_write_barrier(); continue_processing = false; write_barrier(); thread.join(); // wait for thread to exit itself. }

根据CPU和编译器的不同,简单地将continue_processing标记为volatile可能不足以使更改立即发生并对其他线程可见,因此使用了障碍。

你将不得不咨询你的编译器的文档,看看如何创建一个障碍...他们在每一个不同。 VC ++使用_ReadWriteBarrier()和_WriteBarrier() 。

Killing a thread like that is indeed very bad. It's best to give the thread a chance to clean up.

Graceful thread termination is usually done by periodically checking a flag that tells it to exit:

volatile bool continue_processing = true; thread thread; void compile_thread() { while(continue_processing) { // compile one OpenCL program. } } void terminate() { read_write_barrier(); continue_processing = false; write_barrier(); thread.join(); // wait for thread to exit itself. }

Depending on your CPU and compiler, simply marking continue_processing as volatile might not be enough to make the change happen immediately and visible to the other thread, so barriers are used.

You'll have to consult your compiler's documentation to see how to create a barrier... they're different in each one. VC++ uses _ReadWriteBarrier() and _WriteBarrier().

wxwidgets - 以正确的方式退出线程(wxwidgets - exit the thread the right way)

我运行使用wxWidget作为GUI环境的openCL / openGL程序

类的内部对象,派生自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); }

I run openCL /openGL program which uses wxWidget as gui enviroment

Inside object of class ,which derives from wxThread,I perform some complicated calculations and build many openCL programs. I want to delete the thread .But the thread is not deleted immediately – it continue to build programs and just after it finishes with all the compilations.

I know that I can use wxThread::KIll() to exit the thread but it cause some memory problems so its not really an option.

I have myFrame class which is derived from wxFrame.it has pCanvas pointer ,which points to the object which is derived from wxCanvas *pCanvas object includes the myThread (which runs the complicated calculation)

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); }

最满意答案

杀死这样的线程确实非常糟糕。 最好给线程一个清理的机会。

通常通过定期检查一个标志来告诉它退出:

volatile bool continue_processing = true; thread thread; void compile_thread() { while(continue_processing) { // compile one OpenCL program. } } void terminate() { read_write_barrier(); continue_processing = false; write_barrier(); thread.join(); // wait for thread to exit itself. }

根据CPU和编译器的不同,简单地将continue_processing标记为volatile可能不足以使更改立即发生并对其他线程可见,因此使用了障碍。

你将不得不咨询你的编译器的文档,看看如何创建一个障碍...他们在每一个不同。 VC ++使用_ReadWriteBarrier()和_WriteBarrier() 。

Killing a thread like that is indeed very bad. It's best to give the thread a chance to clean up.

Graceful thread termination is usually done by periodically checking a flag that tells it to exit:

volatile bool continue_processing = true; thread thread; void compile_thread() { while(continue_processing) { // compile one OpenCL program. } } void terminate() { read_write_barrier(); continue_processing = false; write_barrier(); thread.join(); // wait for thread to exit itself. }

Depending on your CPU and compiler, simply marking continue_processing as volatile might not be enough to make the change happen immediately and visible to the other thread, so barriers are used.

You'll have to consult your compiler's documentation to see how to create a barrier... they're different in each one. VC++ uses _ReadWriteBarrier() and _WriteBarrier().