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();
}

关于更多

  • QtCreator软件可以找到:

what_find.png

  • 或在以下Qt安装目录找到:
C:\Qt\{你的Qt版本}\Examples\{你的Qt版本}\qtconcurrent\runfunction
  • 相关链接
https://doc.qt.io/qt-5/qtconcurrent-runfunction-example.html
  • Qt君公众号回复『Qt示例』获取更多内容。

猜你喜欢

转载自www.cnblogs.com/qthub/p/12664219.html