Results 1 to 4 of 4

Thread: How to stop QThread?

  1. #1
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to stop QThread?

    Hi, this problem might sound weird, but it got me crazy as I just started using QThread. Here is a short program to test using QThread.

    Qt Code:
    1. #include <QThread>
    2. #include <QCoreApplication>
    3. #include <QDebug>
    4.  
    5. int main (int argc, char** argv) {
    6. QCoreApplication app(argc, argv);
    7. t.start();
    8. if (t.isRunning())
    9. qDebug() << "thread is started";
    10. //t.exit();
    11. //t.quit();
    12. t.terminate();
    13. qDebug() << "try to stop thread";
    14. if(!t.isRunning())
    15. qDebug() << "thread is stopped";
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    I tried exit(), quit() and terminate() and waited for a long time.
    The expected "thread is stopped" never ever shows up (no matter how long I waited). The thread is like running forever. Could anyone help me with this and show me how to properly stop a QThread?

    Thank you.

    Ves

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to stop QThread?

    Re: QThread::quit() & exit() - From the docs:
    This function does nothing if the thread does not have an event loop.
    As for "thread is stopped" you need a delay before "if(!t.isRunning())". See QThread::wait(). Or you could put in a while statement which is essentially what wait() does:
    Qt Code:
    1. t.terminate();
    2. qDebug() << "try to stop thread";
    3.  
    4. while(!t.isFinished()){}
    5.  
    6. if(!t.isRunning())
    7. qDebug() << "thread is stopped";
    To copy to clipboard, switch view to plain text mode 

    HTH

  3. #3
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: How to stop QThread?

    I got it. At least, terminate() can stop the eventloop inside the run(). I am wondering if there is a way actually KILL a Qthread, or it is completely up to the OS to do it.

    Thanks
    Last edited by vespasianvs; 14th March 2010 at 06:34. Reason: typo

  4. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to stop QThread?

    Accordind to Qt doc:

    To stop a thread:
    Execution ends when you return from run(), just as an application does when it leaves main()
    Concerning terminate():
    Warning: This function is dangerous and its use is discouraged. The thread can be terminate at any point in its code path. Threads can be terminated while modifying data. There is no chance for the thread to cleanup after itself, unlock any held mutexes, etc. In short, use this function only if absolutely necessary.
    So you can use a flag inside the run() function when you want to make it stop.
    You set this flag from outside the thread that is checked by the computation within the thread and stop the calculation if the flag is set.

    Qt Code:
    1. void myThread::run()
    2. {
    3. while (! isThreadstopped)
    4. {
    5. // do your threaded stuff;
    6. }
    7.  
    8. } // thread has terminated.
    To copy to clipboard, switch view to plain text mode 
    Last edited by toutarrive; 14th March 2010 at 08:07.

Similar Threads

  1. [QThread] Function calling after thread.stop()
    By Macok in forum Qt Programming
    Replies: 4
    Last Post: 7th February 2009, 14:33
  2. how to stop signals propagation further
    By babu198649 in forum Qt Programming
    Replies: 13
    Last Post: 27th November 2007, 11:25
  3. Replies: 1
    Last Post: 10th October 2007, 11:11
  4. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 08:51
  5. Stop the thread
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2007, 11: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.