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

QFuture<void> Thread is not Starting

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 5 Posters 4.3k 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 Ketan__Patel__0011
    #1

    Hello Friends And QT Experts And Champions

    My New Problem is when I start to run my functions parallelly that time one function in not starting

    My Code Is :

    void MainWindow::StartFunctions()
    {
    QFuture<void> future_1 = QtConcurrent::run(this, &MainWindow::Function_1); /// This Function Is Started Successfully
    
    QFuture<void> future_2 = QtConcurrent::run(this, &MainWindow::Function_2); /// This Function in not Starting For Processing
    }
    

    When i call this function that time my future_1 thread is started but future_2 is not starting

    Please help me solve this problem

    Thanks in advance

    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
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        How do you check / know that the function is not started? Please provide a minimal, compilable example to show your problem.

        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
        3
        • Christian EhrlicherC Christian Ehrlicher

          How do you check / know that the function is not started? Please provide a minimal, compilable example to show your problem.

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

          @Christian-Ehrlicher
          Thanks for reply

          
          void MainWindow::Function_1()
          {
              qDebug() << "START:_Function_1";
                  CheckBool_1 = true;
                  while(CheckBool_1)
                  {
                      QThread::msleep(100);
                      qDebug() << "Function 1 \t Function 1";
                  }  
              qDebug() << "END:_Function_1";
          }
          
          void MainWindow::Function_2()
          {
              qDebug() << "START:_Function_2";
                  CheckBool_2 = true;
                  while(CheckBool_2)
                  {
                      QThread::msleep(100);
                      qDebug() << "Function 2 \t Function 2";
                  }  
              qDebug() << "END:_Function_2";
          }
          
          
          void MainWindow::StartFunctions()
          {
          QFuture<void> future_1 = QtConcurrent::run(this, &MainWindow::Function_1); /// This Function Is Started Successfully
          
          QFuture<void> 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();
                  }
          }
          
          KroMignonK 1 Reply Last reply
          0
          • Ketan__Patel__0011K Ketan__Patel__0011

            @Christian-Ehrlicher
            Thanks for reply

            
            void MainWindow::Function_1()
            {
                qDebug() << "START:_Function_1";
                    CheckBool_1 = true;
                    while(CheckBool_1)
                    {
                        QThread::msleep(100);
                        qDebug() << "Function 1 \t Function 1";
                    }  
                qDebug() << "END:_Function_1";
            }
            
            void MainWindow::Function_2()
            {
                qDebug() << "START:_Function_2";
                    CheckBool_2 = true;
                    while(CheckBool_2)
                    {
                        QThread::msleep(100);
                        qDebug() << "Function 2 \t Function 2";
                    }  
                qDebug() << "END:_Function_2";
            }
            
            
            void MainWindow::StartFunctions()
            {
            QFuture<void> future_1 = QtConcurrent::run(this, &MainWindow::Function_1); /// This Function Is Started Successfully
            
            QFuture<void> 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();
                    }
            }
            
            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #4

            @Ketan__Patel__0011 This can NOT work, and I pretty sure this would also not build.

            You have declared future_1 and future_2 as local variable in MainWindow::StartFunctions()!

            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
            1
            • KroMignonK KroMignon

              @Ketan__Patel__0011 This can NOT work, and I pretty sure this would also not build.

              You have declared future_1 and future_2 as local variable in MainWindow::StartFunctions()!

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

              @KroMignon

              Thanks for reply
              i was already try with global

              jsulmJ KroMignonK 2 Replies Last reply
              0
              • Ketan__Patel__0011K Ketan__Patel__0011

                @KroMignon

                Thanks for reply
                i was already try with global

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

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

                i was already try with global

                Why global?!

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                Ketan__Patel__0011K 1 Reply Last reply
                0
                • Ketan__Patel__0011K Ketan__Patel__0011

                  @KroMignon

                  Thanks for reply
                  i was already try with global

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

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

                  i was already try with global

                  I don't want to blame you, but what are your C/C++ skills?
                  I would also not use globals but class members.

                  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
                  • jsulmJ jsulm

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

                    i was already try with global

                    Why global?!

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

                    @jsulm

                    Because I want to access it anywhere in application

                    that's why I am declare QFuture<void> Globally

                    jsulmJ 1 Reply Last reply
                    0
                    • Ketan__Patel__0011K Ketan__Patel__0011

                      @jsulm

                      Because I want to access it anywhere in application

                      that's why I am declare QFuture<void> Globally

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

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

                      Because I want to access it anywhere in application

                      Sounds like bad design...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      Ketan__Patel__0011K 1 Reply Last reply
                      0
                      • jsulmJ jsulm

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

                        Because I want to access it anywhere in application

                        Sounds like bad design...

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

                        @jsulm

                        so what is the correct way ?

                        help me for it

                        jsulmJ 1 Reply Last reply
                        0
                        • Ketan__Patel__0011K Ketan__Patel__0011

                          @jsulm

                          so what is the correct way ?

                          help me for it

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          @Ketan__Patel__0011 Why do you need access to these variables everywhere in your program?
                          Make these variables members of the class and if other parts of the application REALLY need access to them implement getter methods returning pointers to these variables...

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          Ketan__Patel__0011K 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @Ketan__Patel__0011 Why do you need access to these variables everywhere in your program?
                            Make these variables members of the class and if other parts of the application REALLY need access to them implement getter methods returning pointers to these variables...

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

                            @jsulm
                            I am declare QFuture<void> variable is my class, means it is my class member

                            jsulmJ 1 Reply Last reply
                            0
                            • Ketan__Patel__0011K Ketan__Patel__0011

                              @jsulm
                              I am declare QFuture<void> variable is my class, means it is my class member

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              @Ketan__Patel__0011 Do you mean you changed MainWindow::StartFunctions() ? Because in the code you posted before both variables were local variables.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              Ketan__Patel__0011K 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @Ketan__Patel__0011 Do you mean you changed MainWindow::StartFunctions() ? Because in the code you posted before both variables were local variables.

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

                                @jsulm

                                Yes i changed

                                jsulmJ 1 Reply Last reply
                                0
                                • Ketan__Patel__0011K Ketan__Patel__0011

                                  @jsulm

                                  Yes i changed

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #15

                                  @Ketan__Patel__0011 And does it work now or not?

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  Ketan__Patel__0011K 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @Ketan__Patel__0011 And does it work now or not?

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

                                    @jsulm
                                    Sorry to say it's not working

                                    KroMignonK 1 Reply Last reply
                                    0
                                    • 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

                                            • Login

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