演示如何并發(fā)運行標準函數(shù)猿规。
??QtConcurrent的Run函數(shù)示例演示如何將并發(fā)性應(yīng)用于標準函數(shù),使用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();
}
關(guān)于更多
- 在QtCreator軟件可以找到:
what_find.png
- 或在以下Qt安裝目錄找到:
C:\Qt\{你的Qt版本}\Examples\{你的Qt版本}\qtconcurrent\runfunction
- 相關(guān)鏈接
https://doc.qt.io/qt-5/qtconcurrent-runfunction-example.html
- Qt君公眾號回復(fù)『Qt示例』獲取更多內(nèi)容。