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 can i start a thread after the end of another thread
Forum Updated to NodeBB v4.3 + New Features

How can i start a thread after the end of another thread

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 602 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jagl
    wrote on last edited by
    #1

    how I can start a thread after the end of another thread
    I found this statement:
    future.waitForFinished ();
    and I put it in the second thread but the problem is what do I have to add to :
    QFuture<void> futureSec = QtConcurrent::run(Tab );//this is the second thread

    JonBJ jsulmJ KroMignonK 3 Replies Last reply
    0
    • J jagl

      how I can start a thread after the end of another thread
      I found this statement:
      future.waitForFinished ();
      and I put it in the second thread but the problem is what do I have to add to :
      QFuture<void> futureSec = QtConcurrent::run(Tab );//this is the second thread

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

      @jagl
      I haven't used QFutures, but (a) shouldn't your waitForFinished() be for the first thread you set off, not the second, and (b) shouldn't you only run() the second thread once the first thread has hit waitForFinished(), not before that?

      1 Reply Last reply
      1
      • J jagl

        how I can start a thread after the end of another thread
        I found this statement:
        future.waitForFinished ();
        and I put it in the second thread but the problem is what do I have to add to :
        QFuture<void> futureSec = QtConcurrent::run(Tab );//this is the second thread

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

        @jagl said in How can i start a thread after the end of another thread:

        and I put it in the second thread

        What?
        It is as simple as:

        QFuture<void> futureSec = QtConcurrent::run(Tab );
        futureSec.waitForFinished ();
        

        Keep in mind that this way you're blocking the thread which starts the other thread.
        If you need more control and non-blocking behaviour use QThread and connect a slot to https://doc.qt.io/qt-5/qthread.html#finished signal.

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

        J 1 Reply Last reply
        0
        • jsulmJ jsulm

          @jagl said in How can i start a thread after the end of another thread:

          and I put it in the second thread

          What?
          It is as simple as:

          QFuture<void> futureSec = QtConcurrent::run(Tab );
          futureSec.waitForFinished ();
          

          Keep in mind that this way you're blocking the thread which starts the other thread.
          If you need more control and non-blocking behaviour use QThread and connect a slot to https://doc.qt.io/qt-5/qthread.html#finished signal.

          J Offline
          J Offline
          jagl
          wrote on last edited by jagl
          #4

          @jsulm yes that's why I put it in the function of the second thread
          void Tab ( QFuture<void> future) {
          future.waitForFinished();
          //the rest of the code
          }
          void bateau::on_bat_clicked()
          {
          QFuture<void> future = QtConcurrent::run(Cap);
          QFuture<void> futureSec = QtConcurrent::run(Tab, ??? );//this is the second thread
          }

          JonBJ jsulmJ 2 Replies Last reply
          0
          • J jagl

            @jsulm yes that's why I put it in the function of the second thread
            void Tab ( QFuture<void> future) {
            future.waitForFinished();
            //the rest of the code
            }
            void bateau::on_bat_clicked()
            {
            QFuture<void> future = QtConcurrent::run(Cap);
            QFuture<void> futureSec = QtConcurrent::run(Tab, ??? );//this is the second thread
            }

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

            @jagl
            And how did you change this in response to my earlier reply, telling you what you need to do?

            J 1 Reply Last reply
            0
            • J jagl

              @jsulm yes that's why I put it in the function of the second thread
              void Tab ( QFuture<void> future) {
              future.waitForFinished();
              //the rest of the code
              }
              void bateau::on_bat_clicked()
              {
              QFuture<void> future = QtConcurrent::run(Cap);
              QFuture<void> futureSec = QtConcurrent::run(Tab, ??? );//this is the second thread
              }

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

              @jagl

              QFuture<void> futureSec = QtConcurrent::run(Tab, future);
              

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

              1 Reply Last reply
              0
              • JonBJ JonB

                @jagl
                And how did you change this in response to my earlier reply, telling you what you need to do?

                J Offline
                J Offline
                jagl
                wrote on last edited by
                #7

                @JonB I just want to start the second thread after the end of the first one

                1 Reply Last reply
                0
                • J jagl

                  how I can start a thread after the end of another thread
                  I found this statement:
                  future.waitForFinished ();
                  and I put it in the second thread but the problem is what do I have to add to :
                  QFuture<void> futureSec = QtConcurrent::run(Tab );//this is the second thread

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

                  @jagl said in How can i start a thread after the end of another thread:

                  how I can start a thread after the end of another thread
                  I found this statement:
                  future.waitForFinished ();
                  and I put it in the second thread but the problem is what do I have to add to :
                  QFuture<void> futureSec = QtConcurrent::run(Tab );//this is the second thread

                  I have some difficulties to understand your problem.
                  I suppose Tab is a function you want to start in a dedicated thread.
                  So if you only want to be aware about QtConcurrent::run(), why not simply using a QFutureWatcher?

                  auto watcher = new QFutureWatcher<void>();
                  connect(watcher,  &QFutureWatcher<void>::finished, [watcher, this]() {
                     // do your stuff
                     ...
                     //cleanup
                     watcher->deleteLater();
                  });
                  
                  watcher->setFuture(QtConcurrent::run(Tab));
                  
                  

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

                  J 1 Reply Last reply
                  1
                  • KroMignonK KroMignon

                    @jagl said in How can i start a thread after the end of another thread:

                    how I can start a thread after the end of another thread
                    I found this statement:
                    future.waitForFinished ();
                    and I put it in the second thread but the problem is what do I have to add to :
                    QFuture<void> futureSec = QtConcurrent::run(Tab );//this is the second thread

                    I have some difficulties to understand your problem.
                    I suppose Tab is a function you want to start in a dedicated thread.
                    So if you only want to be aware about QtConcurrent::run(), why not simply using a QFutureWatcher?

                    auto watcher = new QFutureWatcher<void>();
                    connect(watcher,  &QFutureWatcher<void>::finished, [watcher, this]() {
                       // do your stuff
                       ...
                       //cleanup
                       watcher->deleteLater();
                    });
                    
                    watcher->setFuture(QtConcurrent::run(Tab));
                    
                    
                    J Offline
                    J Offline
                    jagl
                    wrote on last edited by
                    #9

                    @KroMignon thanks

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #10

                      @KroMignon's answer is perfectly fine. However, I was thinking if it couldn't be done any easier.

                      Couldn't we drop QFuture/QFutureWatcher altogether? How about:

                      QtConcurrent::run([this]() { Cap(); QtConcurrent::run(Tab); });
                      

                      The only difference is from which thread the second QtConcurrent::run is started. Wouldn't this make it a lot easier? (Does QtConcurrent::run also take a lambda?)

                      KroMignonK 1 Reply Last reply
                      0
                      • S SimonSchroeder

                        @KroMignon's answer is perfectly fine. However, I was thinking if it couldn't be done any easier.

                        Couldn't we drop QFuture/QFutureWatcher altogether? How about:

                        QtConcurrent::run([this]() { Cap(); QtConcurrent::run(Tab); });
                        

                        The only difference is from which thread the second QtConcurrent::run is started. Wouldn't this make it a lot easier? (Does QtConcurrent::run also take a lambda?)

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

                        @SimonSchroeder said in How can i start a thread after the end of another thread:

                        The only difference is from which thread the second QtConcurrent::run is started. Wouldn't this make it a lot easier?

                        Why not, it always depends on the use case.

                        (Does QtConcurrent::run also take a lambda?)

                        For such question, always check documentation ;) ==> https://doc.qt.io/qt-5/qtconcurrentrun.html#using-lambda-functions

                        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