QtConcurrent

Qt之QFuture-CSDN博客

代码来自上面的文章,我这里只是学习一下他的代码!!!他写的很好!

C++面试题记录(Qt上位机方向)_qt上位机开发测试题-CSDN博客

下面这段来自上面的文章!

如果只是普通的任务,没有对象和线程间通信这些,首选QtConcurrent。

直接调用QtConcurrent::run,第一个参数是线程池指针,没有传默认使用全局线程池,之后的可执行对象和参数,可以参考c++11的async,结果使用QFuture接收。

QtConcurrent提供了一个将任务分发到处理器所有的核的易用接口。线程代码完全被隐藏在QtConcurrent框架下,我们不必考虑细节。不能用于线程运行时需要通信或阻塞的情况。

1.

#include <QCoreApplication>
#include <QtConcurrent/QtConcurrentRun>
#include <QDebug>

void hello(const QString &name)
{
    //这里可以是耗时的操作或计算
    qDebug() << "Hello" << name << "from" << QThread::currentThread();
}

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

    qDebug() << "Main Thread" << QThread::currentThread();

    // 在一个单独的线程中调用 hello()
    QFuture<void> f1 = QtConcurrent::run(hello, QString("Qter"));
    QFuture<void> f2 = QtConcurrent::run(hello, QString("Pythoner"));

    // 阻塞调用线程并等待计算完成,确保所有结果可用
    f1.waitForFinished();
    f2.waitForFinished();
}
Main Thread QThread(0x17488080)
Hello "Qter" from QThread(0x1748c940, name = "Thread (pooled)")
Hello "Pythoner" from QThread(0x1748c7a0, name = "Thread (pooled)")

2.可以使用QtConcurrent::mapped批量处理任务

可以暂停计算

再恢复计算

#include <QImage>
#include <QDebug>
#include <QGuiApplication>
#include <QtConcurrent/QtConcurrentMap>

QImage scale(const QImage &image)
{
    qDebug() << "Scaling image in thread" << QThread::currentThread();
    return image.scaled(QSize(100, 100), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    const int imageCount = 20;

    // 创建一个列表包含 imageCount 个图像
    QList<QImage> images;
    for (int i = 0; i < imageCount; ++i)
        images.append(QImage(1600, 1200, QImage::Format_ARGB32_Premultiplied));

    // 使用 QtConcurrent::mapped 对每个图像应用 scale 函数(缩放)
    QFuture<QImage> thumbnails = QtConcurrent::mapped(images, scale);

    // 暂停计算
    thumbnails.pause();
    qDebug() << "isPaused" << thumbnails.isPaused();

    // 恢复计算
    if(thumbnails.isPaused()) {
        thumbnails.resume();
        qDebug() << "isStarted" << thumbnails.isPaused();
    }

    // 阻塞调用线程并等待计算完成,确保所有结果可用
    thumbnails.waitForFinished();

    // 返回 future 的所有结果
    QList<QImage> list = thumbnails.results();
    foreach (QImage image, list) {
        qDebug() << image;
    }

    qDebug() << "********************";

    // 返回 future 的结果数
    int nCount = thumbnails.resultCount();
    for (int i = 0; i < nCount; ++i) {
        QImage image = thumbnails.resultAt(i);
        qDebug() << image;
    }

    return 0;
}

isPaused true

isStarted false

Scaling image in thread QThread(0x1452db28, name = "Thread (pooled)")

Scaling image in thread QThread(0x1452dc08, name = "Thread (pooled)")

Scaling image in thread QThread(0x1452db88, name = "Thread (pooled)")

Scaling image in thread QThread(0x1452dd48, name = "Thread (pooled)")

Scaling image in thread QThread(0x16afbe60, name = "Thread (pooled)")

Scaling image in thread QThread(0x16afbda0, name = "Thread (pooled)")

Scaling image in thread QThread(0x16afbdc0, name = "Thread (pooled)")

Scaling image in thread QThread(0x16afbd80, name = "Thread (pooled)")

Scaling image in thread QThread(0x1452db28, name = "Thread (pooled)")

Scaling image in thread QThread(0x1452dc08, name = "Thread (pooled)")

Scaling image in thread QThread(0x1452db88, name = "Thread (pooled)")

Scaling image in thread QThread(0x16afbe60, name = "Thread (pooled)")

Scaling image in thread QThread(0x16afbda0, name = "Thread (pooled)")

Scaling image in thread QThread(0x1452dd48, name = "Thread (pooled)")

Scaling image in thread QThread(0x16afbdc0, name = "Thread (pooled)")

Scaling image in thread QThread(0x16afbd80, name = "Thread (pooled)")

Scaling image in thread QThread(0x1452db28, name = "Thread (pooled)")

Scaling image in thread QThread(0x1452db88, name = "Thread (pooled)")

Scaling image in thread QThread(0x16afbe60, name = "Thread (pooled)")

Scaling image in thread QThread(0x16afbda0, name = "Thread (pooled)")

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

********************

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

QImage(QSize(100, 100),format=6,depth=32,devicePixelRatio=1,bytesPerLine=400,byteCount=40000)

  • 19
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lpl还在学习的路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值