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. QFuture<void> Thread is not Starting
Qt 6.11 is out! See what's new in the release blog

QFuture<void> Thread is not Starting

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 5 Posters 6.0k 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.
  • Ketan__Patel__0011K Ketan__Patel__0011

    @KroMignon

    i didn't get "START:_Function_2" this message on my debug console.

    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by KroMignon
    #21

    @Ketan__Patel__0011

    Take from QConcurrent::run() documentation:

    Note that function may not run immediately; function will only be run once a thread becomes available.

    I would suggest you to use a dedicated thread pool to ensure to functions would be started in parallel.
    Add a QThreadPool member to your MainWindow class and ensure it has enough threads for you parallel works:

       pool.setMaxThreadCount(2);
    

    And change your code:

    void MainWindow::StartFunctions()
    {
        future_1 = QtConcurrent::run(&pool, this, &MainWindow::Function_1); 
        future_2 = QtConcurrent::run(&pool, this, &MainWindow::Function_2); 
    }
    

    Another improvement would be to check function is not already running:

    void MainWindow::StartFunctions()
    {
        if(!future_1.isRunning())
            future_1 = QtConcurrent::run(&pool, this, &MainWindow::Function_1); 
        if(!future_2 .isRunning())
            future_2 = QtConcurrent::run(&pool, this, &MainWindow::Function_2); 
    }
    

    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

    Ketan__Patel__0011K JonBJ 2 Replies Last reply
    1
    • KroMignonK KroMignon

      @Ketan__Patel__0011

      Take from QConcurrent::run() documentation:

      Note that function may not run immediately; function will only be run once a thread becomes available.

      I would suggest you to use a dedicated thread pool to ensure to functions would be started in parallel.
      Add a QThreadPool member to your MainWindow class and ensure it has enough threads for you parallel works:

         pool.setMaxThreadCount(2);
      

      And change your code:

      void MainWindow::StartFunctions()
      {
          future_1 = QtConcurrent::run(&pool, this, &MainWindow::Function_1); 
          future_2 = QtConcurrent::run(&pool, this, &MainWindow::Function_2); 
      }
      

      Another improvement would be to check function is not already running:

      void MainWindow::StartFunctions()
      {
          if(!future_1.isRunning())
              future_1 = QtConcurrent::run(&pool, this, &MainWindow::Function_1); 
          if(!future_2 .isRunning())
              future_2 = QtConcurrent::run(&pool, this, &MainWindow::Function_2); 
      }
      
      Ketan__Patel__0011K Offline
      Ketan__Patel__0011K Offline
      Ketan__Patel__0011
      wrote on last edited by
      #22

      @KroMignon

      Thanks for Your replay

      single Threadpool member for both QFuture<void> ?

      1 Reply Last reply
      0
      • KroMignonK KroMignon

        @Ketan__Patel__0011

        Take from QConcurrent::run() documentation:

        Note that function may not run immediately; function will only be run once a thread becomes available.

        I would suggest you to use a dedicated thread pool to ensure to functions would be started in parallel.
        Add a QThreadPool member to your MainWindow class and ensure it has enough threads for you parallel works:

           pool.setMaxThreadCount(2);
        

        And change your code:

        void MainWindow::StartFunctions()
        {
            future_1 = QtConcurrent::run(&pool, this, &MainWindow::Function_1); 
            future_2 = QtConcurrent::run(&pool, this, &MainWindow::Function_2); 
        }
        

        Another improvement would be to check function is not already running:

        void MainWindow::StartFunctions()
        {
            if(!future_1.isRunning())
                future_1 = QtConcurrent::run(&pool, this, &MainWindow::Function_1); 
            if(!future_2 .isRunning())
                future_2 = QtConcurrent::run(&pool, this, &MainWindow::Function_2); 
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #23

        @KroMignon said in QFuture<void> Thread is not Starting:

        pool.setMaxThreadCount(2);

        @Ketan__Patel__0011
        It's interesting. If that is indeed needed (I don't doubt it is), https://doc.qt.io/qt-5/qthreadpool.html#maxThreadCount-prop :

        The default maxThreadCount is QThread::idealThreadCount().

        https://doc.qt.io/qt-5/qthread.html#idealThreadCount :

        Returns the ideal number of threads that can be run on the system. This is done querying the number of processor cores, both real and logical, in the system. This function returns 1 if the number of processor cores could not be detected.

        OOI, how many cores do you actually have? What OS are you?

        Ketan__Patel__0011K 1 Reply Last reply
        0
        • JonBJ JonB

          @KroMignon said in QFuture<void> Thread is not Starting:

          pool.setMaxThreadCount(2);

          @Ketan__Patel__0011
          It's interesting. If that is indeed needed (I don't doubt it is), https://doc.qt.io/qt-5/qthreadpool.html#maxThreadCount-prop :

          The default maxThreadCount is QThread::idealThreadCount().

          https://doc.qt.io/qt-5/qthread.html#idealThreadCount :

          Returns the ideal number of threads that can be run on the system. This is done querying the number of processor cores, both real and logical, in the system. This function returns 1 if the number of processor cores could not be detected.

          OOI, how many cores do you actually have? What OS are you?

          Ketan__Patel__0011K Offline
          Ketan__Patel__0011K Offline
          Ketan__Patel__0011
          wrote on last edited by
          #24

          @JonB

          Thanks for Reply

          I have 4 Core in My System
          And I am using Windows 10

          JonBJ 1 Reply Last reply
          0
          • Ketan__Patel__0011K Ketan__Patel__0011

            @JonB

            Thanks for Reply

            I have 4 Core in My System
            And I am using Windows 10

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #25

            @Ketan__Patel__0011
            It would be interesting to hear/confirm what static int QThread::idealThreadCount() returns on your system?

            But to resolve your issue, you should be following what @KroMignon said above, pool.setMaxThreadCount(2);. Confirm that allows your Function_2() to run?

            Ketan__Patel__0011K 3 Replies Last reply
            1
            • JonBJ JonB

              @Ketan__Patel__0011
              It would be interesting to hear/confirm what static int QThread::idealThreadCount() returns on your system?

              But to resolve your issue, you should be following what @KroMignon said above, pool.setMaxThreadCount(2);. Confirm that allows your Function_2() to run?

              Ketan__Patel__0011K Offline
              Ketan__Patel__0011K Offline
              Ketan__Patel__0011
              wrote on last edited by
              #26

              @JonB said in QFuture<void> Thread is not Starting:

              static int QThread::idealThreadCount()

              Where i call this function ?

              Christian EhrlicherC 1 Reply Last reply
              0
              • Ketan__Patel__0011K Ketan__Patel__0011

                @JonB said in QFuture<void> Thread is not Starting:

                static int QThread::idealThreadCount()

                Where i call this function ?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #27

                @Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:

                Where i call this function ?

                It's a static function - therefore you can call it from everywhere - c++ basics. And there is still no minimal, compilable example here btw...

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

                Ketan__Patel__0011K 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:

                  Where i call this function ?

                  It's a static function - therefore you can call it from everywhere - c++ basics. And there is still no minimal, compilable example here btw...

                  Ketan__Patel__0011K Offline
                  Ketan__Patel__0011K Offline
                  Ketan__Patel__0011
                  wrote on last edited by
                  #28

                  @Christian-Ehrlicher

                  I know Static methods can call everywhere

                  but in my case run more than two threads

                  so is this functions return number of cores or number of running threads count ?

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Ketan__Patel__0011
                    It would be interesting to hear/confirm what static int QThread::idealThreadCount() returns on your system?

                    But to resolve your issue, you should be following what @KroMignon said above, pool.setMaxThreadCount(2);. Confirm that allows your Function_2() to run?

                    Ketan__Patel__0011K Offline
                    Ketan__Patel__0011K Offline
                    Ketan__Patel__0011
                    wrote on last edited by Ketan__Patel__0011
                    #29

                    @JonB said in QFuture<void> Thread is not Starting:

                    pool.setMaxThreadCount(2);

                    Yes this is working but when I stop this two thread and again try to start that time Function_2() Is not starting. Only Function_1() Is Start Successfully.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Ketan__Patel__0011
                      It would be interesting to hear/confirm what static int QThread::idealThreadCount() returns on your system?

                      But to resolve your issue, you should be following what @KroMignon said above, pool.setMaxThreadCount(2);. Confirm that allows your Function_2() to run?

                      Ketan__Patel__0011K Offline
                      Ketan__Patel__0011K Offline
                      Ketan__Patel__0011
                      wrote on last edited by
                      #30

                      @JonB said in QFuture<void> Thread is not Starting:

                      static int QThread::idealThreadCount()

                      This Function is return the total core for your system
                      i my system have 4 core

                      so this function return me 4

                      JonBJ 1 Reply Last reply
                      0
                      • Ketan__Patel__0011K Ketan__Patel__0011

                        @Christian-Ehrlicher

                        I know Static methods can call everywhere

                        but in my case run more than two threads

                        so is this functions return number of cores or number of running threads count ?

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #31

                        @Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:

                        so is this functions return number of cores or number of running threads count ?

                        So hard to read the documentation?

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

                        1 Reply Last reply
                        1
                        • Ketan__Patel__0011K Ketan__Patel__0011

                          @JonB said in QFuture<void> Thread is not Starting:

                          static int QThread::idealThreadCount()

                          This Function is return the total core for your system
                          i my system have 4 core

                          so this function return me 4

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #32

                          @Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:

                          so this function return me 4

                          In that case the pool.setMaxThreadCount(2); should not be necessary and will not be the issue. According to the docs. Which you seem to have discovered now by testing. I think.

                          KroMignonK 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:

                            so this function return me 4

                            In that case the pool.setMaxThreadCount(2); should not be necessary and will not be the issue. According to the docs. Which you seem to have discovered now by testing. I think.

                            KroMignonK Offline
                            KroMignonK Offline
                            KroMignon
                            wrote on last edited by
                            #33

                            @JonB said in QFuture<void> Thread is not Starting:

                            In that case the pool.setMaxThreadCount(2); should not be necessary and will not be the issue. According to the docs. Which you seem to have discovered now by testing. I think.

                            This was just a example to ensure calling booth QtConcurrent:run() will always work, because the used thread pool will always have enough thread to start them.

                            Of course you can allocate more threads to this pool, but that is not the point for this issue.
                            My understanding of your issue is that the thread pool you are using does not have free thread to start the new concurrent function.

                            @Ketan__Patel__0011 have you tried what I have suggested? Does it work?

                            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                            Ketan__Patel__0011K 1 Reply Last reply
                            0
                            • KroMignonK KroMignon

                              @JonB said in QFuture<void> Thread is not Starting:

                              In that case the pool.setMaxThreadCount(2); should not be necessary and will not be the issue. According to the docs. Which you seem to have discovered now by testing. I think.

                              This was just a example to ensure calling booth QtConcurrent:run() will always work, because the used thread pool will always have enough thread to start them.

                              Of course you can allocate more threads to this pool, but that is not the point for this issue.
                              My understanding of your issue is that the thread pool you are using does not have free thread to start the new concurrent function.

                              @Ketan__Patel__0011 have you tried what I have suggested? Does it work?

                              Ketan__Patel__0011K Offline
                              Ketan__Patel__0011K Offline
                              Ketan__Patel__0011
                              wrote on last edited by
                              #34

                              @KroMignon

                              Thanks for You Reply

                              Yes I was try you suggested answer and it is working

                              but some time when I try to start both function that time Second function is not run

                              and this problem is not continuously raised

                              it raised sometime

                              for example
                              try to start and stop both function again and again

                              KroMignonK 1 Reply Last reply
                              0
                              • Ketan__Patel__0011K Ketan__Patel__0011

                                @KroMignon

                                Thanks for You Reply

                                Yes I was try you suggested answer and it is working

                                but some time when I try to start both function that time Second function is not run

                                and this problem is not continuously raised

                                it raised sometime

                                for example
                                try to start and stop both function again and again

                                KroMignonK Offline
                                KroMignonK Offline
                                KroMignon
                                wrote on last edited by KroMignon
                                #35

                                @Ketan__Patel__0011 said in QFuture<void> Thread is not Starting:

                                try to start and stop both function again and again

                                As written in documentation, to start a new function there must be an available thread in the used thread pool:

                                Runs function in a separate thread. The thread is taken from the global QThreadPool. Note that function may not run immediately; function will only be run once a thread becomes available.

                                You could check before starting function how many thread are already used:

                                qDebug() << "Allowed thread:" << pool.maxThreadCount() 
                                         <<"/ threads in use:" << pool.activeThreadCount()
                                         << "/ available threads:" << (pool.maxThreadCount()  - pool.activeThreadCount());
                                

                                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                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