Results 1 to 11 of 11

Thread: QThread::isRunning returns true after emitting finished signal

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default QThread::isRunning returns true after emitting finished signal

    After updating Qt 4.7.2 to Qt 4.8.2 I've noticed something strange in QThread behavior. It seems like isRunning returns true even when the thread is finished (the finished signal is already emitted). The following code shows the issue: it prints "the thread is finished, isRunning = true" from the handleThreadFinished slot connected to the thread's finished signal. I've encountered a few new bugs in my application because of this change. Was this behavior intended?

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QTimer>
    3. #include <QThread>
    4. #include <iostream>
    5.  
    6. using namespace std;
    7.  
    8. class MyThread: public QThread
    9. {
    10. protected:
    11. virtual void run()
    12. {
    13. QTimer::singleShot(1000, this, SLOT(quit()));
    14. exec();
    15. //QThread::run();
    16. }
    17. };
    18.  
    19. class Test: public QObject
    20. {
    21. Q_OBJECT
    22.  
    23. public slots:
    24. void run()
    25. {
    26. t = new MyThread;
    27. connect(t, SIGNAL(finished()), this, SLOT(handleThreadFinished()));
    28. t->start();
    29. }
    30.  
    31. private slots:
    32. void handleThreadFinished()
    33. {
    34. cout << "the thread is finished, isRunning = " << (t->isRunning() ? "true" : "false") << endl;
    35. t->deleteLater();
    36. }
    37.  
    38. private:
    39. MyThread *t;
    40. };
    41.  
    42. int main(int argc, char *argv[])
    43. {
    44. QCoreApplication a(argc, argv);
    45. Test test;
    46. QTimer::singleShot(0, &test, SLOT(run()));
    47. return a.exec();
    48. }
    49.  
    50. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread::isRunning returns true after emitting finished signal

    Does this behavior persists if you call start() instead of run()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QThread::isRunning returns true after emitting finished signal

    I am already calling start() for the thread (see the line 28). or did you mean something else?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread::isRunning returns true after emitting finished signal

    Then why do you call run() from your timer in main()?
    Oh now I see... its a bad idea to call the methods with the same names specially when you want to test things like that.
    You also are not terminating the thread but let the application end it.
    This also is a bad idea to test the still running problem.
    Name your methods with meaningful names.
    Stop the thread, have the slot get called by the ending of the thread and test for isRunnning() after that.
    Last edited by high_flyer; 30th July 2012 at 18:52.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QThread::isRunning returns true after emitting finished signal

    I was trying many things and just forgot to change this to the ordinary call. run is a name of my own slot of the Test object (that is not a thread, therefore doesn't have a start method) created for signal/slot communication. However, it doesn't matter in this case how to call run - the issue occurs anyway.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread::isRunning returns true after emitting finished signal

    interesting. does it also return true for isFinished?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QThread::isRunning returns true after emitting finished signal

    interesting. does it also return true for isFinished?
    yes, it does

  8. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread::isRunning returns true after emitting finished signal

    well, plainly, that is silly. How can it be running AND finished? I would report it.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. #9
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QThread::isRunning returns true after emitting finished signal

    well, plainly, that is silly. How can it be running AND finished? I would report it.
    Sorry, i was wrong. In the slot handling finished signal it returns false for isFinished, but isn't it strange also?

    You also are not terminating the thread but let the application end it. This also is a bad idea to test the still running problem.
    The thread is automatically terminated in 1 second. Why should I stop it?

    Stop the thread, have the slot get called by the ending of the thread and test for isRunnning() after that.
    Not sure what you meant. The QThread::finished documentation says "This signal is emitted when the thread has finished executing.". Doesn't it mean it should return false for isRunning and true for isFinished at that moment?

    The updated code:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QTimer>
    3. #include <QThread>
    4. #include <iostream>
    5.  
    6. #include <QLocale>
    7. #include <QStringList>
    8. #include <QDebug>
    9.  
    10. using namespace std;
    11.  
    12. class MyThread: public QThread
    13. {
    14. protected:
    15. virtual void run()
    16. {
    17. QTimer::singleShot(1000, this, SLOT(quit()));
    18. exec();
    19. //QThread::run();
    20. }
    21. };
    22.  
    23. class Test: public QObject
    24. {
    25. Q_OBJECT
    26.  
    27. public slots:
    28. void start()
    29. {
    30. t = new MyThread;
    31. connect(t, SIGNAL(finished()), this, SLOT(handleThreadFinished()), Qt::QueuedConnection);
    32. t->start();
    33. }
    34.  
    35. private slots:
    36. void handleThreadFinished()
    37. {
    38. cout << "the thread is finished, isRunning = " << (t->isRunning() ? "true" : "false") << " isFinished = " << (t->isFinished() ? "true" : "false") << endl;
    39. t->deleteLater();
    40. }
    41.  
    42. private:
    43. MyThread *t;
    44. };
    45.  
    46. int main(int argc, char *argv[])
    47. {
    48. QCoreApplication a(argc, argv);
    49. Test test;
    50. test.start();
    51. return a.exec();
    52. }
    53.  
    54. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread::isRunning returns true after emitting finished signal

    Quote Originally Posted by mentalmushroom View Post
    The thread is automatically terminated in 1 second. Why should I stop it?
    perhaps he means that you 'new' it, but it is never 'delete'd
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  11. #11
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QThread::isRunning returns true after emitting finished signal

    It is deleted in handleThreadFinished slot via deleteLater. I don't close the application in one second, so the thread has enough time to finish. Actually, in this very case I don't really care of thread deletion, I just don't understand why does isRunning return true when finished signal is already emitted.

Similar Threads

  1. Replies: 3
    Last Post: 7th April 2011, 12:09
  2. Replies: 0
    Last Post: 21st October 2010, 12:46
  3. Replies: 1
    Last Post: 22nd July 2010, 10:16
  4. QProcess object not emitting finished(int) signal
    By Tiansen in forum Qt Programming
    Replies: 13
    Last Post: 14th November 2008, 13:17
  5. Replies: 3
    Last Post: 15th April 2007, 20:16

Tags for this Thread

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.