QtConcurrent简单示例

来源:互联网 发布:淘宝网手提电脑 编辑:程序博客网 时间:2024/06/05 04:41
#include <QtCore/QCoreApplication>#include <QtCore\qthread.h>#include <QtCore\QDebug>#include <QtCore\QFuture>#include <QtConcurrent\QtConcurrent>void hello(QString name){    qDebug() << "Hello" << name << " from " << QThread::currentThread();    for (size_t i = 0 ; i < 10 ; i++)    {        QThread::sleep(1);        qDebug("[%s] i = %d", name.data(), i);    }}int main(int argc, char *argv[]){    QCoreApplication a(argc, argv);    QFuture<void> f1 = QtConcurrent::run(hello , QString("A")) ;    QFuture<void> f2 = QtConcurrent::run(hello , QString("B")) ;    f1.waitForFinished();    f2.waitForFinished();    return a.exec();}
0 0
原创粉丝点击