What is the equivalent of std::thread::join in QThread C++

Thank you @Alex F for your suggestion.

Here is what I tried then:

#include <QApplication>

#include <QThread>

#include "mydialog.h"

#include <QDebug>


void foo()


    // simulkate a long work

    qDebug() << " Starting Qthread1 ..." ;




void bar()


    qDebug() << " Starting Qthread2 ..." ;





int main(int argc, char *argv[])


    QApplication a(argc, argv);


    QThread* thread1 = QThread::create(foo);

    QThread* thread2 = QThread::create(bar);


    // to verify at real-time when the thread finishes

    QObject::connect(thread1, &QThread::finished, [](){ qDebug()<< "Thread1 has finished";});

    QObject::connect(thread2, &QThread::finished, [](){ qDebug()<< "Thread2 has finished";});





    qDebug()<< "Waiting for threads to finish";




    qDebug() << "done";


    return a.exec();


The output is as follows:

Waiting for threads to finish
  Starting QThread2 ...
  Strating QThread1 ...
Thread 2 has finished 
Thread 1 has finished

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top