Qt官方示例-并发运行函数

2020-04-09  本文已影响0人  Qt君

演示如何并发运行标准函数。

  QtConcurrent的Run函数示例演示如何将并发性应用于标准函数,使用QFuture实例等待获取返回值。

#include <QDebug>
#include <QThread>
#include <QString>
#include <qtconcurrentrun.h>
#include <QApplication>

using namespace QtConcurrent;

void hello(QString name)
{
    qDebug() << "Hello" << name << "from" << QThread::currentThread();
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QFuture<void> f1 = run(hello, QString("Alice"));
    QFuture<void> f2 = run(hello, QString("Bob"));
    f1.waitForFinished();
    f2.waitForFinished();
}

关于更多

what_find.png
C:\Qt\{你的Qt版本}\Examples\{你的Qt版本}\qtconcurrent\runfunction
https://doc.qt.io/qt-5/qtconcurrent-runfunction-example.html
上一篇 下一篇

猜你喜欢

热点阅读