From 6a95f9170c70ea0f91c233dd1c39b51de9667033 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 27 Jul 2008 16:23:00 +0000 Subject: [PATCH] - Optimized downloadThread mmemory usage --- src/downloadThread.cpp | 18 +++++------------- src/downloadThread.h | 4 ++-- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/downloadThread.cpp b/src/downloadThread.cpp index 9f4d47976..089bd4b64 100644 --- a/src/downloadThread.cpp +++ b/src/downloadThread.cpp @@ -152,9 +152,7 @@ downloadThread::~downloadThread(){ void downloadThread::downloadUrl(QString url){ QMutexLocker locker(&mutex); - if(downloading_list.contains(url)) return; - url_list << url; - downloading_list << url; + urls_queue.enqueue(url); if(!isRunning()){ start(); }else{ @@ -167,8 +165,8 @@ void downloadThread::run(){ if(abort) return; mutex.lock(); - if(url_list.size() != 0 && subThreads.size() < MAX_THREADS){ - QString url = url_list.takeFirst(); + if(!urls_queue.empty() && subThreads.size() < MAX_THREADS){ + QString url = urls_queue.dequeue(); mutex.unlock(); subDownloadThread *st = new subDownloadThread(0, url); subThreads << st; @@ -189,10 +187,7 @@ void downloadThread::propagateDownloadedFile(subDownloadThread* st, QString url, delete st; emit downloadFinished(url, path); mutex.lock(); - index = downloading_list.indexOf(url); - Q_ASSERT(index != -1); - downloading_list.removeAt(index); - if(url_list.size() != 0) { + if(!urls_queue.empty()) { condition.wakeOne(); } mutex.unlock(); @@ -205,10 +200,7 @@ void downloadThread::propagateDownloadFailure(subDownloadThread* st, QString url delete st; emit downloadFailure(url, reason); mutex.lock(); - index = downloading_list.indexOf(url); - Q_ASSERT(index != -1); - downloading_list.removeAt(index); - if(url_list.size() != 0) { + if(!urls_queue.empty()) { condition.wakeOne(); } mutex.unlock(); diff --git a/src/downloadThread.h b/src/downloadThread.h index 752b8f534..15d7a39c0 100644 --- a/src/downloadThread.h +++ b/src/downloadThread.h @@ -30,6 +30,7 @@ #include #include #include +#include class subDownloadThread : public QThread { Q_OBJECT @@ -55,8 +56,7 @@ class downloadThread : public QThread { Q_OBJECT private: - QStringList url_list; - QStringList downloading_list; + QQueue urls_queue; QMutex mutex; QWaitCondition condition; bool abort;