Skip to content

std thread

amin-amani edited this page Aug 9, 2020 · 1 revision
#include <QCoreApplication>
#include <QtDebug>
#include <QThread>
#include <thread>
using namespace  std;

class Calculation
{
public:
    Calculation()
    {

    }
    //=============================================
    int Add(int a,int b)
    {
        qDebug()<<"working"<<a+b;
        QThread::msleep(1000);
        qDebug()<<"done ...";
        return  a+b;
    }
};

int DOCalc(int a,int b)
{
    qDebug()<<"working"<<a+b;
    QThread::msleep(1000);
    qDebug()<<"done ...";
    return  a+b;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Calculation p;
    std::thread thr(DOCalc,1,2);
    auto future = std::async(&Calculation::Add,&p, 5,6);
    int res=future.get();
    thr.join();
    qDebug()<<"call after"<<res;
    return a.exec();
}

Clone this wiki locally