Results 1 to 7 of 7

Thread: want to get thread to stop when application exits

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default want to get thread to stop when application exits

    I need to get a worker thread to clean up and exit when the application exits. The thread normally would continue to function forever(sending serial messages) but when someone clicks the X in the upper right of the application window, I need the thread to finish. Currently, I have to use the windows task manager to kill the thread, which is not acceptable long term.

    I'm using the following line to spawn the thread:
    QFuture<void> future = QtConcurrent::run(monitor_thread);

    I thought all I need to do is set a flag, then the thread will see the flag and clean up, but I'm having trouble understanding where to set the flag. I put it in the MainWindow destructor(see below), but for some reason it's not working. Maybe I should handle the signal for the exit event, but I'm not sure how. Any suggestions would be appreciated.

    MainWindow::~MainWindow()
    {
    app_trying_to_exit = true;
    delete ui;
    }

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: want to get thread to stop when application exits

    How and how often is your thread function looking at the app_trying_to_exit flag?

  3. The following user says thank you to ChrisW67 for this useful post:

    dan146 (2nd May 2012)

  4. #3
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: want to get thread to stop when application exits

    Maybe QCoreApplication::aboutToQuit() signal may help.
    ~ We are nothing in this universe ~

  5. The following user says thank you to viulskiez for this useful post:

    dan146 (2nd May 2012)

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: want to get thread to stop when application exits

    Quote Originally Posted by viulskiez View Post
    Maybe QCoreApplication::aboutToQuit() signal may help.
    It won't help because the thread doesn't have an event loop.

    For long running threads such as this I would advise against using threads from the thread pool (which is what QtConcurrent::run() does) because on single cpu systems this might starve the application.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    dan146 (2nd May 2012)

  8. #5
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: want to get thread to stop when application exits

    In response to Chrisw67, the thread checks this flag right after the gui thread releases a lock, so the destructor actually looks like this:

    MainWindow::~MainWindow()
    {

    fprintf(dfp,"\n\n=================EXITING APPLICATION====================\n\n");
    app_trying_to_exit = true;
    sysmutex.unlock();
    delete ui;
    }

    Note I don't see the output from fprintf in my log file.


    In response to Wysota, I will look into non thread pool threads. Note the thread waits on a lock for 99% of the time, so maybe starving the app this isn't a problem.

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: want to get thread to stop when application exits

    It is, because the thread is created and reserved. The fact that it sleeps is meaningless. If you try to start another thread from the pool that has only one thread, the thread will not start until the first one exits (which happens when the application is destroyed) so in practice the second thread will not run at all. You would have to release the thread before going to sleep on the mutex and reserve it back again when you are woken up. However it simply makes no sense to complicate things that much in this particular case since it's much easier to use QThread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    dan146 (2nd May 2012)

  11. #7
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: want to get thread to stop when application exits

    Sorry for the slow reply, I'm juggling 3 projects. Yes I used QThread and it works great now! The flag is now seen by the thread and it exits properly.
    Thanks!

Similar Threads

  1. howto Processing GUI events until a thread exits
    By doggrant in forum Qt Programming
    Replies: 0
    Last Post: 5th October 2009, 15:50
  2. [QThread] Function calling after thread.stop()
    By Macok in forum Qt Programming
    Replies: 4
    Last Post: 7th February 2009, 13:33
  3. ¿How to stop thread that is capturing from a webcam?
    By JoseTlaseca in forum Qt Programming
    Replies: 8
    Last Post: 28th August 2008, 04:45
  4. Stop the thread during recursivly loading directory
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2007, 19:02
  5. Stop the thread
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2007, 10:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.