Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Terminating QThread properly
Qt 6.11 is out! See what's new in the release blog

Terminating QThread properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.9k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    Narveth
    wrote on last edited by
    #1

    Hello,

    I have a multithread application and what I basically want is to kill a thread during its run by sending it a signal from another QObject.
    I have simplifed the code like bellow :

    0_1515761597455_thread qt.png

    The output given is :

    Going for sleep Worker(0x193cfb0)
    Waiking up
    Going for sleep Worker(0x193cfb0)
    Killing thread QThread(0x193ad00)
    Waiking up
    Going for sleep Worker(0x193cfb0)
    Waiking up
    Going for sleep Worker(0x193cfb0)
    Waiking up
    Going for sleep Worker(0x193cfb0)
    Waiking up

    The problem I have is that the thread is not really killed exactly when the signal is sent ... I know I can use terminate() but its not recommended and it is too abrupt (in my application before terminate the thread I would like to do some operations then emit a result). I could also use requestInterruption() and add in the worker for loop an isInterruptionRequest but I want to kill the blocking operation if any is running (here its a sleep but it could be a wait on QNetworkAccessManager request).

    I would like to know if there is any way to do what I want to do ?

    S 1 Reply Last reply
    0
    • N Narveth

      Hello,

      I have a multithread application and what I basically want is to kill a thread during its run by sending it a signal from another QObject.
      I have simplifed the code like bellow :

      0_1515761597455_thread qt.png

      The output given is :

      Going for sleep Worker(0x193cfb0)
      Waiking up
      Going for sleep Worker(0x193cfb0)
      Killing thread QThread(0x193ad00)
      Waiking up
      Going for sleep Worker(0x193cfb0)
      Waiking up
      Going for sleep Worker(0x193cfb0)
      Waiking up
      Going for sleep Worker(0x193cfb0)
      Waiking up

      The problem I have is that the thread is not really killed exactly when the signal is sent ... I know I can use terminate() but its not recommended and it is too abrupt (in my application before terminate the thread I would like to do some operations then emit a result). I could also use requestInterruption() and add in the worker for loop an isInterruptionRequest but I want to kill the blocking operation if any is running (here its a sleep but it could be a wait on QNetworkAccessManager request).

      I would like to know if there is any way to do what I want to do ?

      S Offline
      S Offline
      samdol
      wrote on last edited by samdol
      #2

      @Narveth
      How about put a stop flag in run()?
      As a member variable of Worker class, define

      bool stop;
      ...
      stop = false;
      void stopThread() {stop = true;}
      ...
      for(int i=0;i<5;++i){
          if(stop){
              Do whatever you want.
              break;
          }
       ...
      }
      

      And you can stop thread by
      running stopThread().

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Narveth
        wrote on last edited by
        #3

        Thanks for the reply,

        But as I said I would like to stop the current operation the thread is actually doing. So imagine instead of Qthread::sleep(5) I have QThread::sleep(500000) and the signal is sent right after the sleep has begun, my thread will stop after the sleep and not during. I would like to interrupt the sleep and I dont know if its possible.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          If you have to sleep such a long time then you might not be using the right technology to handle such a wait. A QWaitCondition might be more suited.

          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
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved