qt线程池(转)

转自:https://www.jianshu.com/p/2fff70ad81a2

Qt多线程:QtConcurrent + QFuture + QFutureWatcher

QtConcurrent实际是一个命名空间,该命名空间提供了高级API,从而可以在不使用低级线程原语(启动线程、线程间同步、锁等)的情况下编写多线程程序。

但是QtConcurrent仅支持接受纯函数或者lambda表达式,不支持信号和槽,如果需要监听任务执行结果可以通过与QFuture和QFutureWatcher配合来达到。

QFuture类表示异步计算的结果,使用Qt Concurrent框架中的API启用。

QFuture允许线程针对一个或多个结果进行同步,这些结果将在稍后的时间点准备就绪。结果可以是具有默认构造函数和副本构造函数的任何类型。如果在调用result(),resultAt()或results()函数时结果不可用,则QFuture将等待,直到结果变为可用。您可以使用isResultReadyAt()函数来确定结果是否准备就绪。对于报告多个结果的QFuture对象,resultCount()函数返回连续结果的数量。这意味着将结果从0迭代到resultCount()始终是安全的。
QFuture提供了Java样式的迭代器(QFutureIterator)和STL样式的迭代器(QFuture :: const_iterator)。使用这些迭代器是将来获取结果的另一种方法。
QFuture还提供了与运行计算进行交互的方法。例如,可以使用cancel()函数取消计算。要暂停计算,请使用setPaused()函数或pause(),resume()或togglePaused()便利函数之一。请注意,并非所有异步计算都可以取消或暂停。例如,不能取消QtConcurrent :: run()返回的future。但是QtConcurrent :: mappedReduced()返回的Future可以。
进度信息由progressValue(),progressMinimum(),progressMaximum()和progressText()函数提供。 waitForFinished()函数使调用线程阻塞并等待计算完成,确保所有结果均可用。
可以使用isCanceled(),isStarted(),isFinished(),isRunning()或isPaused()函数查询QFuture表示的计算状态。
QFuture是轻量级引用计数类,可以按值传递。
QFuture <void>专门用于不包含任何结果获取功能。任何QFuture <T>都可以分配或复制到QFuture <void>中。如果仅需要状态或进度信息,而无需实际结果数据,则此功能很有用。
 
 

要使用信号和插槽与正在运行的任务进行交互,则需要使用QFutureWatcher。

QFutureWatcher类允许使用信号和插槽监视QFuture。

QFutureWatcher提供有关QFuture的信息和通知。使用setFuture()函数开始监视特定的QFuture。 future()函数返回带有setFuture()的Future集。
为了方便起见,QFutureWatcher中还提供了一些函数:progressValue(),progressMinimum(),progressMaximum(),progressText(),isStarted(),isFinished(),isRunning(),isCanceled(),isPaused(),waitForFinished (),result()和resultAt()。 以及一些槽函数cancel(),setPaused(),pause(),resume()和togglePaused()。
状态更改通过started(),finished(),canceled(),paused(),resumed(),resultReadyAt()和resultReadyAt()信号进行报告。从progressRangeChanged(),void progressValueChanged()和progressTextChanged()信号提供进度信息。
节流控制由setPendingResultsLimit()函数提供。当待处理的resultReadyAt()或resultsReadyAt()信号的数量超过限制时,由Future表示的计算将自动受到限制。一旦待处理信号的数量下降到限制以下,计算将恢复。


#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <QtConcurrent>

#include <QThread>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :

    QMainWindow(parent),

    ui(new Ui::MainWindow)

{

    ui->setupUi(this);

    m_pWatcher = new(std::nothrow) QFutureWatcher<int>(this);

    connect(m_pWatcher, &QFutureWatcher<int>::progressValueChanged, [](int nVal){

        qDebug() << "watcher => " << QThread::currentThreadId() << QThread::currentThread() << " " << nVal << endl;

    });

    connect(m_pWatcher, &QFutureWatcher<int>::resultReadyAt, [this](int index){

        qDebug() << "Result At " << index << " is " << m_pWatcher->resultAt(index) << endl;

    });

}

MainWindow::~MainWindow()

{

    delete ui;

}

int func(int nVal)

{

    qDebug() << "start => " << QThread::currentThreadId() << QThread::currentThread() << " " << nVal << endl;

    return nVal *2;

}

void MainWindow::on_btnStart_clicked()

{

    QList<int> lst;

    lst.push_back(1);

    lst.push_back(2);

    lst.push_back(3);

    lst.push_back(4);

    lst.push_back(5);

    lst.push_back(6);

    m_pWatcher->setFuture(QtConcurrent::mapped(lst, func));

}

  

通过运行结果可以发现,QtConccurent管理的线程实际是从线程池分配线程资源的,而绑定QFutureWatcher的槽是在主线程中执行的。
在需要单次执行且内部逻辑较简单的时候使用QtConccurrent+QFuture+QFutureWatcher是很方便的,可以减少很多编码工作量,而且在多cpu环境中,QtConccurent也会启用多核。

 
作者:阿丹呐
链接:https://www.jianshu.com/p/2fff70ad81a2
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 
补充:
还有一种方法,继承 qrunnable   ,如果子线程需要用到信息和槽,在继承qrunnable之前还需继承qobject ,添加 Q_Object 宏定义  。然后在qthreadpool的调用     start(继承qrunnable的线程对象指针)。
 

参考:https://blog.csdn.net/y396397735/article/details/78637634

参考:https://www.cnblogs.com/sherlock-lin/articles/11708966.html




原文地址:https://www.cnblogs.com/qijunzifeng/p/13846884.html