From: Vadim Zeitlin Date: Sun, 21 Sep 2003 23:54:47 +0000 (+0000) Subject: terminate child threads in frame dtor, not in OnQuit() (otherwise they're left runnin... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/4e5bbd4022f82c5aab6aaa80330465db5f9c9223 terminate child threads in frame dtor, not in OnQuit() (otherwise they're left running if we just close the window) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@23786 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/samples/thread/thread.cpp b/samples/thread/thread.cpp index 3e65bde45d..06c569c1cf 100644 --- a/samples/thread/thread.cpp +++ b/samples/thread/thread.cpp @@ -75,6 +75,7 @@ class MyFrame: public wxFrame public: // ctor MyFrame(wxFrame *frame, const wxString& title, int x, int y, int w, int h); + virtual ~MyFrame(); // operations void WriteText(const wxString& text) { m_txtctrl->WriteText(text); } @@ -428,6 +429,51 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title, } +MyFrame::~MyFrame() +{ + // NB: although the OS will terminate all the threads anyhow when the main + // one exits, it's good practice to do it ourselves -- even if it's not + // completely trivial in this example + + // tell all the threads to terminate: note that they can't terminate while + // we're deleting them because they will block in their OnExit() -- this is + // important as otherwise we might access invalid array elements + wxThread *thread; + + wxGetApp().m_critsect.Enter(); + + // check if we have any threads running first + const wxArrayThread& threads = wxGetApp().m_threads; + size_t count = threads.GetCount(); + + if ( count ) + { + // set the flag for MyThread::OnExit() + wxGetApp().m_waitingUntilAllDone = TRUE; + + // stop all threads + while ( ! threads.IsEmpty() ) + { + thread = threads.Last(); + + wxGetApp().m_critsect.Leave(); + + thread->Delete(); + + wxGetApp().m_critsect.Enter(); + } + } + + wxGetApp().m_critsect.Leave(); + + if ( count ) + { + // now wait for them to really terminate + wxGetApp().m_semAllDone.Wait(); + } + //else: no threads to terminate, no condition to wait for +} + MyThread *MyFrame::CreateThread() { MyThread *thread = new MyThread(this); @@ -597,50 +643,6 @@ void MyFrame::OnIdle(wxIdleEvent& event) void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) { - // NB: although the OS will terminate all the threads anyhow when the main - // one exits, it's good practice to do it ourselves -- even if it's not - // completely trivial in this example - - // tell all the threads to terminate: note that they can't terminate while - // we're deleting them because they will block in their OnExit() -- this is - // important as otherwise we might access invalid array elements - { - wxThread *thread; - - wxGetApp().m_critsect.Enter(); - - // check if we have any threads running first - const wxArrayThread& threads = wxGetApp().m_threads; - size_t count = threads.GetCount(); - - if ( count ) - { - // set the flag for MyThread::OnExit() - wxGetApp().m_waitingUntilAllDone = TRUE; - - // stop all threads - while ( ! threads.IsEmpty() ) - { - thread = threads.Last(); - - wxGetApp().m_critsect.Leave(); - - thread->Delete(); - - wxGetApp().m_critsect.Enter(); - } - } - - wxGetApp().m_critsect.Leave(); - - if ( count ) - { - // now wait for them to really terminate - wxGetApp().m_semAllDone.Wait(); - } - //else: no threads to terminate, no condition to wait for - } - Close(TRUE); }