March 24, 2011

lucky lucky
Lab Rat
5 posts

Stop all threads

 

I need to complete the work threads before closing the application. I have done so:

  1. connect(quitAction, SIGNAL(triggered()), this, SLOT(quit()));
  2.  
  3. ...........
  4.  
  5. void MainWindow::quit()
  6. {
  7.    thread_update_base.exit();
  8.    thread_update_base.wait();
  9.    qApp->quit();
  10. }

So it will be correct in terms of security? If not, please show how to correctly stop all threads.

5 replies

March 24, 2011

dialingo dialingo
Lab Rat
158 posts

It depends on what is going on in thread_update_base.
Your code does not look bad actually. You stop the event loop and then you politely wait.
However you need to ask yourself if your thread needs additional cleanup and if it is ok to stop the thread on termination of any method which happens to run when you call exit().
If you do not have an objection go ahead. Otherwise it is better to send a signal to the worker thread, pick the signal up, do the cleanup and then you exit().

March 24, 2011

lucky lucky
Lab Rat
5 posts

Thank you.

March 24, 2011

Andre Andre
Area 51 Engineer
6031 posts

You might consider using a maximum wait time.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 24, 2011

florent.revelut florent.reve..
Lab Rat
103 posts

If you have more than one thread to terminate, keep in mind that terminating one thread might trigger som event to be handled by another one…
To keep it simple, I would advise to keep a drawing of interaction, to know in which order terminate and when to wait. Definitely, not as easy as it sounds :-)

March 24, 2011

dialingo dialingo
Lab Rat
158 posts

When the timer times out the Terminator enters the scene. Hasta la vista baby. The application may be severly hit. Continueing from here is dangerous and can lead to the land of pain.
Was that too dramatic?

 
  ‹‹ Qt project file .pro      dll/dylib correct way to copy/install/etc.. ? ››

You must log in to post a reply. Not a member yet? Register here!