[PictureLoader] Use thread pool instead of creating new thread (#6072)

* delete threads

* Run reply processing in thread pool
This commit is contained in:
RickyRister 2025-08-15 02:11:39 -03:00 committed by GitHub
parent 38f76d449a
commit 1649f30389
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 13 deletions

View file

@ -94,7 +94,7 @@ QNetworkReply *PictureLoaderWorker::makeRequest(const QUrl &url, PictureLoaderWo
QNetworkReply *reply = networkManager->get(req);
// Connect reply handling
connect(reply, &QNetworkReply::finished, worker, [reply, worker] { worker->handleNetworkReply(reply); });
connect(reply, &QNetworkReply::finished, worker, [reply, worker] { worker->acceptNetworkReply(reply); });
return reply;
}

View file

@ -11,6 +11,7 @@
#include <QNetworkDiskCache>
#include <QNetworkReply>
#include <QThread>
#include <QThreadPool>
// Card back returned by gatherer when card is not found
static const QStringList MD5_BLACKLIST = {"db0c48db407a907c16ade38de048a441"};
@ -28,16 +29,7 @@ PictureLoaderWorkerWork::PictureLoaderWorkerWork(const PictureLoaderWorker *work
// Hook up signals to settings
connect(&SettingsCache::instance(), SIGNAL(picDownloadChanged()), this, SLOT(picDownloadChanged()));
pictureLoaderThread = new QThread;
moveToThread(pictureLoaderThread);
connect(pictureLoaderThread, &QThread::started, this, &PictureLoaderWorkerWork::startNextPicDownload);
// clean up threads once loading finishes
connect(this, &QObject::destroyed, pictureLoaderThread, &QThread::quit);
connect(pictureLoaderThread, &QThread::finished, pictureLoaderThread, &QObject::deleteLater);
pictureLoaderThread->start(QThread::LowPriority);
startNextPicDownload();
}
void PictureLoaderWorkerWork::startNextPicDownload()
@ -78,6 +70,15 @@ void PictureLoaderWorkerWork::picDownloadFailed()
}
}
/**
* Processes the reply in another thread.
* @param reply The finished reply. Takes ownership of the object
*/
void PictureLoaderWorkerWork::acceptNetworkReply(QNetworkReply *reply)
{
QThreadPool::globalInstance()->start([this, reply] { handleNetworkReply(reply); });
}
/**
*
* @param reply The reply. Takes ownership of the object

View file

@ -30,14 +30,14 @@ public:
PictureToLoad cardToDownload;
public slots:
void handleNetworkReply(QNetworkReply *reply);
void acceptNetworkReply(QNetworkReply *reply);
private:
QThread *pictureLoaderThread;
bool picDownload;
void startNextPicDownload();
void picDownloadFailed();
void handleNetworkReply(QNetworkReply *reply);
void handleFailedReply(const QNetworkReply *reply);
void handleSuccessfulReply(QNetworkReply *reply);
QImage tryLoadImageFromReply(QNetworkReply *reply);