Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFuture<void> Thread is not Starting
Qt 6.12 LTS 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 8.6k 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 Offline
    Ketan__Patel__0011K Offline
    Ketan__Patel__0011
    wrote on last edited by
    #17

    Let Me Share My Whole Code

    mainwindow.h

    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    private:
        void Function_1();
        void Function_2();
        void StartFunctions();
    private slots:
        void on_BtnStart_clicked();
        void on_BtnStop_clicked();
    private:
         bool CheckBool_1;
         bool CheckBool_2;
        Ui::MainWindow *ui;
        QFuture<void> future_1 ;
        QFuture<void> future_2 ;
    };
    

    mainwindow.cpp

    void MainWindow::StartFunctions()
    {
    future_1 = QtConcurrent::run(this, &MainWindow::Function_1); /// This Function Is Started Successfully
    
    future_2 = QtConcurrent::run(this, &MainWindow::Function_2); /// This Function in not Starting For Processing
    }
    
    void MainWindow::on_BtnStart_clicked() //// START
    {
         StartFunctions();
    }
    
    void MainWindow::on_BtnStop_clicked() //// Stop
    {
        CheckBool_1 = false;
            if(future_1 ->isRunning())  
            {
                 future_1->setPaused(true);
                 future_1->cancel();
            }
    
        CheckBool_2 = false;
            if(future_2->isRunning())  
            {
                 future_2->setPaused(true);
                 future_2->cancel();
            }
    }
    
    1 Reply Last reply
    0
    • Ketan__Patel__0011K Ketan__Patel__0011

      @jsulm
      Sorry to say it's not working

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

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

      Sorry to say it's not working

      What do you mean with "not working"?
      As far as I can see from your code, on Function_1() start, you should see "START:_Function_1" on debug output and "START:_Function_2" on Function_2() start.

      Do you have any of those message on debug console?

      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

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

        Sorry to say it's not working

        What do you mean with "not working"?
        As far as I can see from your code, on Function_1() start, you should see "START:_Function_1" on debug output and "START:_Function_2" on Function_2() start.

        Do you have any of those message on debug console?

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

        @KroMignon

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

        KroMignonK 2 Replies Last reply
        0
        • 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
          #20

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

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

          And when you invert the lines in StartFunctions() to start Function_2() before Function_1()?

          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
          • 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 Online
                        Christian EhrlicherC Online
                        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 Online
                                Christian EhrlicherC Online
                                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
                                        • Unsolved