jeudi 29 octobre 2020

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

How can I wait for the finish of a QThread or What is the equivalent of std::thread::join() method in QThread

In std::thread, I can do it like the following

#include <iostream>
#include <thread>
#include <chrono>
 
void foo()
{
    // simulate expensive operation
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
void bar()
{
    // simulate expensive operation
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
int main()
{
    std::cout << "starting first thread executing foo\n";
    std::thread thread1(foo);
 
    std::cout << "starting second thread executing bar \n";
    std::thread thread2(bar);
 
    std::cout << "waiting for threads to finish..." << std::endl;
    thread1.join();
    thread2.join();
 
    std::cout << "done!\n";
}

The output gives the following output

Aucun commentaire:

Enregistrer un commentaire