Results 1 to 10 of 10

Thread: QThread : how to stop an infinite loop

  1. #1
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QThread : how to stop an infinite loop

    I execute in a Qthread an image processing procedure taking around 3 hours without the possibility to put some check points inside it for a exit gate. The problem is I cannot stop it. This code represents this problem :
    Qt Code:
    1. class Toto : public QObject
    2. {
    3. Q_OBJECT
    4. public slots:
    5. void exec(){
    6. //I represent the real process with an infinite loop
    7. while(1==1);
    8. }
    9. };
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication aa(argc, argv);
    13. QThread * t1 = new QThread;
    14. Toto * toto1 = new Toto;
    15. QThread * t2 = new QThread;
    16. Toto * toto2 = new Toto;
    17. push.show();
    18. toto1->moveToThread(t1);
    19. toto2->moveToThread(t2);
    20. QMetaObject::invokeMethod(toto1, "exec", Qt::QueuedConnection);
    21. QMetaObject::invokeMethod(toto2, "exec", Qt::QueuedConnection);
    22. QApplication::connect(&push, SIGNAL(pressed()), t1,SLOT(terminate()), Qt::QueuedConnection);
    23. QApplication::connect(&push, SIGNAL(pressed ()), t2,SLOT(terminate()), Qt::QueuedConnection);
    24. t2->start();
    25. t1->start();
    26. return aa.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 
    The problem is my infinite loop is not interrupted when I press the buttom.
    Do you have any idea to solve this problem?
    Thanks,
    Vincent

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread : how to stop an infinite loop

    Hi,

    Maybe the way you are using QThread is not the correct.

    I had the same problem once and this is how I made it, maybe is not the cleanest way:

    Maybe your code in the thread goes through a big loop of process. I would put those process in the run() of a QThread (subclasing QThread). In that code you can put a variable called stopnow. That if its true the loop will break. Then you can set that variable to true out of the thread with a slot called stopIt(). !!Use a mutex before changing its value. And thats it... If you call stopit, it will stop.

    Carlos.
    Last edited by qlands; 24th February 2012 at 14:23.

  3. #3
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread : how to stop an infinite loop

    Thanks Carlos for your reactivity
    However, my infinite loop represents more than two hundreds algorithms of my image processing library. I cannot add a break point for each one. In other multi-threading library, we can kill a thread as a "pirate" without a flag.
    So...
    Last edited by TarielVincent; 24th February 2012 at 14:45.

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread : how to stop an infinite loop

    ok. So maybe is that such code is not in run() then stop() does not work. Try by subclassing QThread and put that code in run(). Then start the thread with start(). Those many pointers and metaobjects invokes are really don't needed.

    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. public:
    4. void run();
    5. };
    6.  
    7. void MyThread::run()
    8. {
    9. //My massive process here
    10. }
    To copy to clipboard, switch view to plain text mode 

    Carlos

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

    TarielVincent (24th February 2012)

  6. #5
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread : how to stop an infinite loop

    Thanks
    Maybe your code in the thread goes through a big loop of process. I would put those process in the run() of a QThread (subclasing QThread). In that code you can put a variable called stopnow. That if its true the loop will break. Then you can set that variable to true out of the thread with a slot called stopIt(). !!Use a mutex before changing its value. And thats it... If you call stopit, it will stop.
    Thanks Carlos but as I said, the infinite loop was just a representation of my image processing algorithms. I cannot changed all algorithms for that...
    Last edited by TarielVincent; 24th February 2012 at 15:56.

  7. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread : how to stop an infinite loop

    Quote Originally Posted by TarielVincent View Post
    However even if the infinite loop (the massive process) is in the run, you cannot call the slot terminate() signal because you do not call the thread's event loop.
    Actually, that's not entirely true.

    From documentation:
    void QThread::terminate () [slot]
    Warning: This function is dangerous and its use is discouraged. The thread can be terminated 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.
    You're not mentioning what system you're using, on windows your example works exacly as you expect it to work, thread is terminated as soon as the button is pressed.
    If you're using linux then it may not be the case, doc mention this as well:
    void QThread::terminate () [slot]
    Terminates the execution of the thread. The thread may or may not be terminated immediately, depending on the operating systems scheduling policies. Use QThread::wait() after terminate() for synchronous termination.

  8. The following user says thank you to Spitfire for this useful post:

    TarielVincent (24th February 2012)

  9. #7
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread : how to stop an infinite loop

    Thanks Spitfire
    Actually, I was aware of this fact The thread can be terminated at any point in its code path. It is why I implement my algorithms like that. But I didn't have the expected result.
    I am in linux.
    Last edited by TarielVincent; 24th February 2012 at 16:06.

  10. #8
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread : how to stop an infinite loop

    I'm not an expert in linux so I'm not sure how you can deal with it.
    What I can suggest if there's no other options is to make the image processing library a commandline tool and launch it in a QProcess rather than QThread.
    This way you will be able to kill it by deleting the process or calling system kill command.


    Added after 23 minutes:


    Or try using pthread_kill() or something similar.
    Last edited by Spitfire; 24th February 2012 at 16:41.

  11. #9
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread : how to stop an infinite loop

    My image processing library is included in Graphical User Interface. With my colleague, we choose this solution. if after n-seconds,the thread is still running. We display a pop-up to inform that a thread cannot stop and a restart is required with Qprocess little bit as your solution.
    Thanks

  12. #10
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread : how to stop an infinite loop

    Hi if you are doing some image processing then using QThread is not recommended.
    Anyway if you insist on QThread usage you shouldn't subclass QThread at all. See this article where author of QThread class explains how it should be done and way most people are sub-classing (history reason).

    Anyway the best thing to use is Qt Cuncurent framework. If you have task which can be split to pieces the you application will scale autocratically depending on number of cores available.

Similar Threads

  1. Infinite loop in QXmlSchemaValidator::validate()?
    By TropicalPenguin in forum Qt Programming
    Replies: 0
    Last Post: 9th November 2010, 16:09
  2. infinite loop
    By zakis in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2009, 18:52
  3. Replies: 4
    Last Post: 19th August 2009, 20:38
  4. Infinite loop - resize parent from child
    By bitChanger in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2006, 14:21
  5. is it possible to stay on a user defined infinite loop?
    By mahe2310 in forum Qt Programming
    Replies: 9
    Last Post: 24th March 2006, 15:29

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.