Qt文档阅读笔记-QFuture官方解析及实例

QFurture是异步进行的,可以开1个或多个线程。返回值可以是任意类型的。当调用result(), resultAt(), results()返回值无效时QFuture将会等待result返回正常为止。可以使用isResultReadAt()函数去判断是否有数据。QFuture返回值可以是多个,使用resultCount()函数可以得到其数量。

还有很多函数就不一一介绍了。直接在代码中演示吧!

 

下面是一个官方例子:

runfunction.pro

QT += concurrent widgets
CONFIG += cmdline

SOURCES += main.cpp

target.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/runfunction
INSTALLS += target

main.cpp

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

程序运行截图如下:

下面是处理大量数据的例子:

源码如下:

FutureDemo.pro

QT -= gui
QT += concurrent

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

main.cpp

#include <QCoreApplication>
#include <QtConcurrent>
#include <QTimer>
#include <QTime>
#include <QEventLoop>
#include <QVector>
#include <QMapIterator>
#include <QString>
#include <QDebug>
#include <QMap>

using namespace QtConcurrent;


QVector<int> MyTest(QVector<int> vec){

    qDebug() << "deal with vec !  " << QThread::currentThread();

    QVector<int> ret;

    for(int i = 0; i < vec.size(); i++){

        ret << vec[i] - vec[i];
    }
    qDebug() << "deal with vec !  finished" << QThread::currentThread();

    return ret;
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));

    QVector<int> oriData;
    for(int i = 0; i < 1000000; i++){

        oriData.append(qrand() % 50000);
    }

    qDebug() << "随机数生成完成!";

    QVector<int> overData;
    QList<QFuture<QVector<int>>> list;

    for(int i = 0; i < 1000000; i+= 10000){

        QFuture<QVector<int>> future = run(MyTest, oriData.mid(i, 10000));
        list.append(future);
    }

    QEventLoop loop;
    QTimer::singleShot(2 * 1000, &loop, &QEventLoop::quit);
    loop.exec();


    for(int i = 0; i < list.size(); i++){

        list[i].waitForFinished();
        overData.append(list[i]);
    }


    qDebug() << "over";
    return a.exec();
}

运行截图如下:

变量

 

 

©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值