Thursday, April 24, 2014

Qt::Concurrent, Multi-Thread under QT programming

Concurrency was always pain in the ass for my short programming career, however under QT C++, there is a easy trick to complete this. It never been this easy, hence I want to share it.
QTConcurrent is what you need, and here is a short example.




#include <QCoreApplication>
#include <QtConcurrent/QtConcurrent>

int plusFunction(int number);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int number = 0;
    QFuture<int> future = QtConcurrent::run(plusFunction, number);
    QTextStream(stdout) << future.result() << endl;
    return a.exec();
}


int plusFunction(int number) {
    return number + 1;
}

No comments:

Post a Comment