文章目錄
- 一、QtConcurrent 簡介
- 二、常用功能分類
- 2.1 異步運行一個函數(無返回值)
- 2.2 異步運行一個帶參數的函數(有返回值)
- 2.3 綁定類成員函數
- 2.4 容器并行處理(map)
- 三、線程池控制
- 四、取消任務
- 五、典型應用場景
- 六、完整示例:并發下載圖片
一、QtConcurrent 簡介
QtConcurrent 是 Qt 提供的一個高級并發編程模塊,屬于 QtConcurrent 命名空間,旨在簡化多線程任務的執行。它支持并行執行算法(如 map、filter、reduce),還支持異步任務運行和結果管理,不需要顯式管理 QThread。
模塊介紹
頭文件包含:
#include <QtConcurrent>
模塊依賴:
QT += concurrent
常用功能:
二、常用功能分類
2.1 異步運行一個函數(無返回值)
QtConcurrent::run([]() {qDebug() << "后臺線程中執行任務:" << QThread::currentThread();
});
2.2 異步運行一個帶參數的函數(有返回值)
int add(int a, int b) {return a + b;
}QFuture<int> future = QtConcurrent::run(add, 3, 5);// 后續獲取結果
int result = future.result(); // 阻塞直到完成
qDebug() << "結果是:" << result;
2.3 綁定類成員函數
class Worker {
public:int multiply(int x, int y) {return x * y;}
};Worker worker;
QFuture<int> future = QtConcurrent::run(&worker, &Worker::multiply, 4, 6);
2.4 容器并行處理(map)
并發修改容器元素(原地修改)
QList<int> numbers = {1, 2, 3, 4, 5};auto doubleIt = [](int &n) {n *= 2;
};QtConcurrent::map(numbers, doubleIt);// 輸出: 2 4 6 8 10
并發映射容器到新容器(mapped)
QList<int> numbers = {1, 2, 3};auto square = [](int n) {return n * n;
};QList<int> squares = QtConcurrent::mapped(numbers, square).results();
// 輸出: 1 4 9
并發過濾(filtered)
QStringList names = {"Alice", "Bob", "Eve"};auto isShort = [](const QString &name) {return name.length() <= 3;
};QStringList shortNames = QtConcurrent::filtered(names, isShort).results();
// 輸出: Bob, Eve
2.5 使用 QFutureWatcher 監聽結果(推薦與 UI 配合)
QFutureWatcher<QString> *watcher = new QFutureWatcher<QString>(this);connect(watcher, &QFutureWatcher<QString>::finished, this, [=]() {QString result = watcher->result();qDebug() << "計算完成,結果為:" << result;
});QFuture<QString> future = QtConcurrent::run([]() {QThread::sleep(2);return QString("Hello from thread");
});watcher->setFuture(future);
三、線程池控制
QtConcurrent 默認使用全局 QThreadPool,可通過如下方式調整:
QThreadPool::globalInstance()->setMaxThreadCount(8);
或者使用局部線程池:
QThreadPool pool;
QtConcurrent::run(&pool, someFunction);
四、取消任務
可通過 QFuture 中的 cancel() 方法取消任務:
QFuture<void> future = QtConcurrent::run([]{for (int i = 0; i < 100; ++i) {if (QThread::currentThread()->isInterruptionRequested()) {return;}QThread::msleep(100);}
});// 取消任務
future.cancel();
五、典型應用場景
六、完整示例:并發下載圖片
效果目標:
核心類:ImageDownloader
// ImageDownloader.h#ifndef IMAGEDOWNLOADER_H
#define IMAGEDOWNLOADER_H#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSemaphore>class ImageDownloader : public QObject
{Q_OBJECT
public:explicit ImageDownloader(QObject *parent = nullptr);void setMaxConcurrent(int count); // 設置最大并發數void downloadImages(const QStringList &urls, const QString &saveDir);signals:void imageDownloaded(const QString &url, const QString &filePath);void downloadFailed(const QString &url, const QString &error);void allFinished();private:void downloadOne(const QString &url, const QString &saveDir, int retryCount = 3);QSemaphore m_semaphore; // 控制并發數int m_maxConcurrent = 5;
};#endif // IMAGEDOWNLOADER_H
// ImageDownloader.cpp#include "ImageDownloader.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QNetworkRequest>
#include <QThread>
#include <QDebug>
#include <QtConcurrent>ImageDownloader::ImageDownloader(QObject *parent): QObject(parent), m_semaphore(m_maxConcurrent)
{}void ImageDownloader::setMaxConcurrent(int count)
{m_maxConcurrent = count;m_semaphore = QSemaphore(m_maxConcurrent);
}void ImageDownloader::downloadImages(const QStringList &urls, const QString &saveDir)
{for (const QString &url : urls) {QtConcurrent::run([=]() {m_semaphore.acquire(); // 限制并發downloadOne(url, saveDir);m_semaphore.release();});}// 可選:全部下載完再發信號(略)
}void ImageDownloader::downloadOne(const QString &urlStr, const QString &saveDir, int retryCount)
{QNetworkAccessManager manager;QNetworkRequest request(QUrl(urlStr));QNetworkReply *reply = manager.get(request);QEventLoop loop;QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);loop.exec();if (reply->error() == QNetworkReply::NoError) {QByteArray data = reply->readAll();QUrl url(urlStr);QString fileName = QFileInfo(url.path()).fileName();QString fullPath = QDir(saveDir).filePath(fileName);QFile file(fullPath);if (file.open(QIODevice::WriteOnly)) {file.write(data);file.close();emit imageDownloaded(urlStr, fullPath);} else {emit downloadFailed(urlStr, "文件保存失敗");}} else {if (retryCount > 0) {qDebug() << "下載失敗,重試:" << urlStr;downloadOne(urlStr, saveDir, retryCount - 1);} else {emit downloadFailed(urlStr, reply->errorString());}}reply->deleteLater();
}
使用示例:
ImageDownloader *downloader = new ImageDownloader(this);
downloader->setMaxConcurrent(5); // 最多5張圖同時下載QStringList urls = {"https://example.com/a.jpg","https://example.com/b.jpg","https://example.com/c.jpg"
};QString savePath = QDir::currentPath() + "/images";connect(downloader, &ImageDownloader::imageDownloaded, this, [](const QString &url, const QString &path){qDebug() << "下載成功:" << url << " -> " << path;
});connect(downloader, &ImageDownloader::downloadFailed, this, [](const QString &url, const QString &error){qWarning() << "下載失敗:" << url << error;
});downloader->downloadImages(urls, savePath);