Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Terminate QThread correctly

    General and Desktop
    thread qt4.8 yocto dizzy
    7
    11
    14291
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mrjj
      mrjj Lifetime Qt Champion last edited by

      Hi
      How do you call
      ThreadWorker::onAbort() ?

      if not via signal & slot then you might need to wrap
      thread_exit in a std::atomic<bool> to avoid race conditions.

      A 1 Reply Last reply Reply Quote 1
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        If you have an infinite loop, you should rather have a stop method that allows you to exit the loop cleanly. Terminating threads is never a good idea and should only be a last resort solution.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 3
        • A
          Andrea @mrjj last edited by Andrea

          @mrjj the onAbort() slot is called via signal, with QueuedConnection, since the emitter and receiver live in different threads.
          @SGaist there is not a "stop" method, but the "onAbort()" method does same logical operation: it just sets the bool condition checked in the loop to exit from this latter one. Maybe I could be wrong, but to me it seems this is a clean way to exit thread loop, isn't it?
          Mmm..but when I'm going to close the application, terminate a thread seems to be a good way to follow before closing the whole application or should I just close the application a let the OS "manage" threads created? Thanks

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @Andrea last edited by

            @Andrea After calling onAbort() do you actually wait for the threads to finish? See http://doc.qt.io/qt-5.9/qthread.html#wait

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            A 1 Reply Last reply Reply Quote 0
            • J.Hilk
              J.Hilk Moderators @Andrea last edited by

              @Andrea

              Usually you call quit to quit a thread:
              Try this.

              connect(qApp, &QApplication::aboutToQuit,   threadController, [=]{
                  threadController->quit();
                  threadController->wait();
              });
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply Reply Quote 2
              • A
                Andrea @jsulm last edited by

                @jsulm if I put a wait() after "calling" onAbort() method (via signal-slot), hte program hang waiting the thread to finish...but this never happens. I'm sure the thread exits its while loop (I've used qDebug() to check this), but calling wait function of thread controller never returns.

                J.Hilk 1 Reply Last reply Reply Quote 0
                • J.Hilk
                  J.Hilk Moderators @Andrea last edited by

                  @Andrea
                  wait() will return when either of these 2 conditions are met:

                  • The thread associated with this QThread object has finished execution (i.e. when it returns from run()). This function will return true if the thread has finished. It also returns true if the thread has not been started yet.
                  • time milliseconds has elapsed. If time is ULONG_MAX (the default), then the wait will never timeout (the thread must return from run()). This function will return false if the wait timed out.

                  the default time is ULONG_MAX which is 4294967295 ms -> roughly 50 days

                  you never call quit on threadController, onAbort only effects your worker object, not the thread wrapper.

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply Reply Quote 3
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Sorry, I missed the implementation of onAbort while looking at the code.

                    Note that that name is a bit confusing. It rather sounds like something that would be called when the thread has aborted.

                    IIRC, boolean variable used like that might be "optimised". I'd replace it with a QAtomicInt.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply Reply Quote 0
                    • JKSH
                      JKSH Moderators @Andrea last edited by

                      @Andrea said in Terminate QThread correctly:

                      @jsulm if I put a wait() after "calling" onAbort() method (via signal-slot), hte program hang waiting the thread to finish...but this never happens. I'm sure the thread exits its while loop (I've used qDebug() to check this), but calling wait function of thread controller never returns.

                      Hi @Andrea,

                      You stopped the loop in ThreadWorker, but you didn't stop the loop in QThread.

                      As @J-Hilk said, you need to call threadController->quit() too.

                      @Andrea said in Terminate QThread correctly:

                      I've read the QThread Basics docs and also had a look on StarckOverflow and the Qt forum, but still having the doubt.

                      Also read through http://doc.qt.io/qt-5/qthread.html#details and http://doc.qt.io/qt-5/threads-technologies.html. There are 2 ways to use QThread:

                      1. Create a worker object
                      2. Subclass QThread and override QThread::run().

                      If you want your thread to receive signals, you should use a worker object. However, if you want to run an infinite loop (while( !this->thread_exit ) { /*...*/ }) and you don't need signals/slots, then it's simpler to subclass QThread.

                      Judging by your code example, I don't think you need a worker object. Also, if you subclass QThread, the thread will quit when your while loop stops.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply Reply Quote 2
                      • Buckwheat
                        Buckwheat last edited by

                        Hi @Andrea

                        WOW! Infinite loop to just sleep and process events. Why not just start a timer of PreciseTimer and allow the events to flow freely in the thread? Since you want it to run as fast as possible you can even set the timeout to 0 to run freely when there are no events.

                        You can then look at thread->requestInterruption () and the thread will comply! Just make sure if you are doing something in a loop in the timer callback you check thread ()->isInterruptionRequested (). And best of all... NO CHECKING THE EVENT QUEUE!

                        There is a nice writeup about proper thread use at: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                        You are basically there with your code. Just the loop is wasteful.

                        Dave Fileccia

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post