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 
#include 
#include 
#include 
#include 

using namespace QtConcurrent;

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

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

程序运行截图如下:

Qt文档阅读笔记-QFuture官方解析及实例_第1张图片

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

源码如下:

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace QtConcurrent;


QVector MyTest(QVector vec){

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

    QVector 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 oriData;
    for(int i = 0; i < 1000000; i++){

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

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

    QVector overData;
    QList>> list;

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

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

运行截图如下:

Qt文档阅读笔记-QFuture官方解析及实例_第2张图片

变量

Qt文档阅读笔记-QFuture官方解析及实例_第3张图片

 

 

你可能感兴趣的:(Qt,文档阅读笔记,QFuture,Qt,QtConcurrent)