- // 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
- {
- 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 )
- {
- // we do, ask them to stop
- for ( size_t n = 0; n < count; n++ )
- {
- threads[n]->Delete();
- }
-
- // set the flag for MyThread::OnExit()
- wxGetApp().m_waitingUntilAllDone = TRUE;
- }
-
- wxGetApp().m_critsect.Leave();
-
- if ( count )
- {
- // now wait for them to really terminate but leave the GUI mutex
- // before doing it as otherwise we might dead lock
- wxMutexGuiLeave();
-
- wxGetApp().m_condAllDone.Wait();
-
- wxMutexGuiEnter();
- }
- //else: no threads to terminate, no condition to wait for
- }
-
- Close(TRUE);