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. How to for QThread and QThreadPool objects wait for thread creation?
QtWS25 Last Chance

How to for QThread and QThreadPool objects wait for thread creation?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 3.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.
  • A Offline
    A Offline
    AlekseyB
    wrote on 17 Jun 2018, 13:41 last edited by
    #1

    After creating tasks for QThread and QThreadPool objects, the main thread must wait for them to run to use the wait / waitForDone methods (otherwise it will finish before the worker threads start).

    How to do it?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 17 Jun 2018, 13:44 last edited by
      #2

      Please be a little bit more precise - what do you want to achieve and why do you want to wait until a thread is running?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply 17 Jun 2018, 13:59
      0
      • C Christian Ehrlicher
        17 Jun 2018, 13:44

        Please be a little bit more precise - what do you want to achieve and why do you want to wait until a thread is running?

        A Offline
        A Offline
        AlekseyB
        wrote on 17 Jun 2018, 13:59 last edited by
        #3

        @Christian-Ehrlicher becouse I want to use wait / waitForDone methods (otherwise it will finish before the worker threads start).

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 17 Jun 2018, 17:09 last edited by
          #4

          I still don't understand your problem. A QQthread emits finished() when it's done. Just connect on this signal and then do whatever you want (e.g. quit your app).

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          A 1 Reply Last reply 18 Jun 2018, 06:39
          4
          • C Christian Ehrlicher
            17 Jun 2018, 17:09

            I still don't understand your problem. A QQthread emits finished() when it's done. Just connect on this signal and then do whatever you want (e.g. quit your app).

            A Offline
            A Offline
            AlekseyB
            wrote on 18 Jun 2018, 06:39 last edited by
            #5

            @Christian-Ehrlicher
            Main- thread create worker- threads and must be wait while their end of execution. For this I use wait() method, but wait() return true in two case: before starting thread and after end execution. In my program first case done and I have close program without any execution worker- threads.

            M 1 Reply Last reply 18 Jun 2018, 06:50
            0
            • A AlekseyB
              18 Jun 2018, 06:39

              @Christian-Ehrlicher
              Main- thread create worker- threads and must be wait while their end of execution. For this I use wait() method, but wait() return true in two case: before starting thread and after end execution. In my program first case done and I have close program without any execution worker- threads.

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 18 Jun 2018, 06:50 last edited by
              #6

              @AlekseyB
              Hi
              Pleases show some code.
              Normally one would use the finished() signal to terminate the program calling
              quit() on main thread.

              It sounds your design is more about preventing main() to run out of scope ?
              Do you execute app.exec() ?

              A 1 Reply Last reply 18 Jun 2018, 07:29
              0
              • M mrjj
                18 Jun 2018, 06:50

                @AlekseyB
                Hi
                Pleases show some code.
                Normally one would use the finished() signal to terminate the program calling
                quit() on main thread.

                It sounds your design is more about preventing main() to run out of scope ?
                Do you execute app.exec() ?

                A Offline
                A Offline
                AlekseyB
                wrote on 18 Jun 2018, 07:29 last edited by
                #7

                @mrjj For QThreadPool nothing finished() signal.
                For my object, developed by inheritance from QThreadPool, I write this methods for waiting all worker- threads:

                void syncPool::WaitForFinish()
                {
                    while(!waitForDone(300))
                    {
                        //process events
                        QApplication::processEvents();
                    }
                }
                

                This methods interruption by main- thread for waiting worker- threads, but after creating tasks worker- threads do not have time to create, and this methods close without waiting of execution.

                But if I add count QuantityAllTasks and write this:

                void syncPool::WaitForFinish()
                {
                    while(QuantityAllTasks > 0)
                    {
                        //process events
                        QApplication::processEvents();
                
                        waitForDone(300);
                    }
                }
                

                , program successfully run worker- thread and execute them. I not to want add any shared count in my multi- threaded code.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Buckwheat
                  wrote on 18 Jun 2018, 13:47 last edited by kshegunov
                  #8

                  Hi @AlekseyB ,

                  Instead of using QThreadPool directly, use QFuture (http://doc.qt.io/qt-5/qfuture.html) and QFutureWatcher (http://doc.qt.io/qt-5/qfuturewatcher.html). The nice thing about QFuture is using QFutureWatcher that has signals 'started' and 'finished.' You can asynchronously know when your threads start and stop. You can also watch containers full of results! Futures can also return results. Here is a little sample:

                  connect (m_watcher, &QFutureWatcher <bool>::finished, this, &threadFinished);
                   ...
                  QFuture <bool> future = QtConcurrent::run (this, &func, arg1, ...);
                  

                  You can now check result or watch for later...

                  if (future.result ()) { // This will wait until the thread finishes.
                  }
                  

                  or

                  m_watcher->setFuture (future);
                   ...
                  
                  bool MyObject::func (arg1, ...)
                  {
                     if (fail) return false;
                     else return true;
                  }
                  
                  
                  void MyObject::threadFinished ()
                  {
                     QFutureWatcher <bool>* watcher = dynamic_cast <QFutureWatcher <bool>*) (sender ());
                  
                     if (watcher && watcher->result ()) {
                     }
                  }
                  

                  QtConcurrent::run acts like connect and you can use QObject derived classes for worker functions in the run method. I typically use this for running my plugin based file importers.

                  [Added code tags ~kshegunov]

                  Dave Fileccia

                  A 1 Reply Last reply 20 Jun 2018, 08:06
                  4
                  • B Buckwheat
                    18 Jun 2018, 13:47

                    Hi @AlekseyB ,

                    Instead of using QThreadPool directly, use QFuture (http://doc.qt.io/qt-5/qfuture.html) and QFutureWatcher (http://doc.qt.io/qt-5/qfuturewatcher.html). The nice thing about QFuture is using QFutureWatcher that has signals 'started' and 'finished.' You can asynchronously know when your threads start and stop. You can also watch containers full of results! Futures can also return results. Here is a little sample:

                    connect (m_watcher, &QFutureWatcher <bool>::finished, this, &threadFinished);
                     ...
                    QFuture <bool> future = QtConcurrent::run (this, &func, arg1, ...);
                    

                    You can now check result or watch for later...

                    if (future.result ()) { // This will wait until the thread finishes.
                    }
                    

                    or

                    m_watcher->setFuture (future);
                     ...
                    
                    bool MyObject::func (arg1, ...)
                    {
                       if (fail) return false;
                       else return true;
                    }
                    
                    
                    void MyObject::threadFinished ()
                    {
                       QFutureWatcher <bool>* watcher = dynamic_cast <QFutureWatcher <bool>*) (sender ());
                    
                       if (watcher && watcher->result ()) {
                       }
                    }
                    

                    QtConcurrent::run acts like connect and you can use QObject derived classes for worker functions in the run method. I typically use this for running my plugin based file importers.

                    [Added code tags ~kshegunov]

                    A Offline
                    A Offline
                    AlekseyB
                    wrote on 20 Jun 2018, 08:06 last edited by AlekseyB
                    #9

                    @Buckwheat said in How to for QThread and QThreadPool objects wait for thread creation?:
                    Buckwheat,

                    Thank you!
                    But In your case I have no process events. My worker- threads send messages to main- thread for information about current stage of working.

                    Start- signal is emitted when this QFutureWatcher starts watching the future set with setFuture(). But this no mean that thread is start working.

                    I use wait- methods, but they're use my processor to 100%. Why? My post devote by this question was unduly closed (may be bad question ban on this forum). I see that is the other question, but I have no choice and ask here.

                    kshegunovK 1 Reply Last reply 21 Jun 2018, 19:05
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 20 Jun 2018, 21:36 last edited by
                      #10

                      Hi,

                      You didn't answer the question: why do you need to block your main thread ?

                      If you don't want your user to do something before some state is reached, then disable the GUI or put a modal "waiting to be finished" dialog but don't block the event loop.

                      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
                      2
                      • B Offline
                        B Offline
                        Buckwheat
                        wrote on 21 Jun 2018, 14:30 last edited by
                        #11

                        @AlekseyB

                        So your worker needs to communicate with the main thread. Yet another reason NOT to wait! If you have to call "processEvents" then you should connect to QFutureWatcher::finished and react when it is done.

                        You do understand that if you block the main thread, your signals to the main thread will not be processed! Signals across thread boundaries are handled via the event queue. Take @SGaist advice and disable your UI on start of thread and enable on thread finished if you do not want the user to interact with your application while the thread is running.

                        My typical usage is for file geodetic model loading which could take up to 30 seconds. I cannot have the hardware stop being processed during this time but I do NOT want the user to do anything. So, I disable the main window on startup and re-enable on finished. This allows the main application to still process hardware events from the CAN and serial ports.

                        Dave Fileccia

                        1 Reply Last reply
                        2
                        • A AlekseyB
                          20 Jun 2018, 08:06

                          @Buckwheat said in How to for QThread and QThreadPool objects wait for thread creation?:
                          Buckwheat,

                          Thank you!
                          But In your case I have no process events. My worker- threads send messages to main- thread for information about current stage of working.

                          Start- signal is emitted when this QFutureWatcher starts watching the future set with setFuture(). But this no mean that thread is start working.

                          I use wait- methods, but they're use my processor to 100%. Why? My post devote by this question was unduly closed (may be bad question ban on this forum). I see that is the other question, but I have no choice and ask here.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on 21 Jun 2018, 19:05 last edited by kshegunov
                          #12

                          @AlekseyB said in How to for QThread and QThreadPool objects wait for thread creation?:

                          I use wait- methods, but they're use my processor to 100%. Why?

                          You never answered what's this waitForDone and how it is implemented. Also note that calling some function waitFor***, won't make it blocking. As I mentioned in your other thread, you can use a semaphore for waiting:

                          // # thread 1 
                          semaphore.acquire(); //< We block here until someone calls release(), or we continue on if someone already did
                          
                          // # thread 2
                          semaphore.release(); //< Free the waiting thread if it's blocked
                          

                          My post devote by this question was unduly closed (may be bad question ban on this forum).

                          Unduly? I don't think so. We don't discriminate in this forum, the question may be bad, may be good, it's still a question. However we do try to keep the place tidy, which excludes posting of multiples of the same question.

                          I see that is the other question, but I have no choice and ask here.

                          Which is exactly why I closed your other thread, as the issues you describe as two different problems are rather one.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          2

                          4/12

                          17 Jun 2018, 17:09

                          topic:navigator.unread, 8
                          • Login

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