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. QThread - threads how to kill all of them?
Forum Update on Monday, May 27th 2025

QThread - threads how to kill all of them?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qthreadsthreads
14 Posts 4 Posters 11.5k Views
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    Why not make re-usable objects ? And use e.g. QThreadPool ?

    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
    0
    • Q Offline
      Q Offline
      qDebug
      wrote on last edited by
      #5

      I cant try. Is there any code example?

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

        Do you mean like the one provided in the details of QThreadPool's documentation ?

        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
        0
        • Q Offline
          Q Offline
          qDebug
          wrote on last edited by
          #7

          A real world example not some code fragments that shows how to start and end all the threads.

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

            If you have a fixed number of threads then just keep a list of them and stop them when needed.

            if you make your worker object you can make it re-usuable so you only have to restart the thread.

            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
            0
            • Q Offline
              Q Offline
              qDebug
              wrote on last edited by
              #9

              How do i keep "a list" and how can i use this list to stop the threads? This is exactly my problem.

              Paul ColbyP 1 Reply Last reply
              0
              • Q qDebug

                How do i keep "a list" and how can i use this list to stop the threads? This is exactly my problem.

                Paul ColbyP Offline
                Paul ColbyP Offline
                Paul Colby
                wrote on last edited by
                #10

                @qDebug said in QThread - threads how to kill all of them?:

                How do i keep "a list" and how can i use this list to stop the threads?

                Instead of:

                thread = new QThread;
                ...
                myThreads.append(thread->currentThread());
                

                which is adding the controlling thread each time, add the child threads instead, like:

                thread = new QThread;
                ...
                myThreads.append(thread);
                

                Then you can do something like:

                foreach (QThread * thread, myThreads) {
                    thread->terminate();
                }
                

                Cheers.

                Q 1 Reply Last reply
                2
                • Paul ColbyP Paul Colby

                  @qDebug said in QThread - threads how to kill all of them?:

                  How do i keep "a list" and how can i use this list to stop the threads?

                  Instead of:

                  thread = new QThread;
                  ...
                  myThreads.append(thread->currentThread());
                  

                  which is adding the controlling thread each time, add the child threads instead, like:

                  thread = new QThread;
                  ...
                  myThreads.append(thread);
                  

                  Then you can do something like:

                  foreach (QThread * thread, myThreads) {
                      thread->terminate();
                  }
                  

                  Cheers.

                  Q Offline
                  Q Offline
                  qDebug
                  wrote on last edited by qDebug
                  #11

                  @Paul-Colby Thanks you, that helps.

                  But in general i'm not sure if i use threads in the best known way yet. I got the thread, a worker class and inside the worker class i start a QFtp download. So all the worker classes show as child the QFtp. In my case i guess i have to stop the QFtp download. The download stops if i stop the worker. So far so good.

                  Not sure right now if i even need a worker class for QFtp. Can i just move the QFtp download to the thread?

                  So... do i loop through all the threads or the worker classes and call the abort function i've created to stop the QFtp download? I really can't find any examples about that particular problem.

                  Thanks!

                  Paul ColbyP kshegunovK 2 Replies Last reply
                  0
                  • Q qDebug

                    @Paul-Colby Thanks you, that helps.

                    But in general i'm not sure if i use threads in the best known way yet. I got the thread, a worker class and inside the worker class i start a QFtp download. So all the worker classes show as child the QFtp. In my case i guess i have to stop the QFtp download. The download stops if i stop the worker. So far so good.

                    Not sure right now if i even need a worker class for QFtp. Can i just move the QFtp download to the thread?

                    So... do i loop through all the threads or the worker classes and call the abort function i've created to stop the QFtp download? I really can't find any examples about that particular problem.

                    Thanks!

                    Paul ColbyP Offline
                    Paul ColbyP Offline
                    Paul Colby
                    wrote on last edited by
                    #12

                    @qDebug said in QThread - threads how to kill all of them?:

                    But in general i'm not sure if i use threads in the best known way yet. ... Not sure right now if i even need a worker class for QFtp.

                    I'm not at all familiar with QFtp, but a few notes that may help:

                    • QFtp is no longer exported in Qt5, so as per the Qt5 docs, I'd recommend you use the QNetworkAccessManager class directly if that's possible for you.
                    • with both classes, it should be possible to do the download asynchronously without using any threads. The only reason I'd use worker threads here, is if you had to do any real processing of the data as it was arriving, or you have some slick UI animation going on and don't want any stutters / artefacts.
                    • if you can use QNetworkAccessManager, then have a look at either Network Download Example or Network Download Manager Example, neither of which require threads.
                    • otherwise check out the (older) FTP Example, which also doesn't require threads.

                    Good luck :)

                    1 Reply Last reply
                    0
                    • Q qDebug

                      @Paul-Colby Thanks you, that helps.

                      But in general i'm not sure if i use threads in the best known way yet. I got the thread, a worker class and inside the worker class i start a QFtp download. So all the worker classes show as child the QFtp. In my case i guess i have to stop the QFtp download. The download stops if i stop the worker. So far so good.

                      Not sure right now if i even need a worker class for QFtp. Can i just move the QFtp download to the thread?

                      So... do i loop through all the threads or the worker classes and call the abort function i've created to stop the QFtp download? I really can't find any examples about that particular problem.

                      Thanks!

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #13

                      @qDebug said in QThread - threads how to kill all of them?:

                      But in general i'm not sure if i use threads in the best known way yet.

                      If you need to call QThread::terminate then you're doing something very wrong. I like to say that this (doiung forceful termination) is the equivalent of hitting your girl with baseball bat after having a nice dinner. :)

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      1
                      • Q Offline
                        Q Offline
                        qDebug
                        wrote on last edited by
                        #14

                        After a lot of try and error, mostly error and crashes, i ended up with this working solution:

                        foreach(FTPDownload* w, g_Worker)
                        {
                                if(w->_working)
                                {
                                    w->abort(); // sets _working = false and _abort = true
                                    w->ftp->deleteLater(); // ends the QFtp download
                                    w->thread()->quit();
                                    w->thread()->deleteLater();
                                }
                        }
                        

                        The only problem left is when i remove the table from the QTableView the progress update wont terminate and the app crashes, because the table does not exist anymore.

                        I guess i have to add the the function that executes the code from above somehow in a event loop so the code after that deletes the table wont be executed before all the downloads are really stopped. Right?

                        1 Reply Last reply
                        0

                        • Login

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