Browse Source

- Optimized downloadThread mmemory usage

adaptive-webui-19844
Christophe Dumez 17 years ago
parent
commit
6a95f9170c
  1. 18
      src/downloadThread.cpp
  2. 4
      src/downloadThread.h

18
src/downloadThread.cpp

@ -152,9 +152,7 @@ downloadThread::~downloadThread(){ @@ -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(){ @@ -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, @@ -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 @@ -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();

4
src/downloadThread.h

@ -30,6 +30,7 @@ @@ -30,6 +30,7 @@
#include <QWaitCondition>
#include <QStringList>
#include <curl/curl.h>
#include <QQueue>
class subDownloadThread : public QThread {
Q_OBJECT
@ -55,8 +56,7 @@ class downloadThread : public QThread { @@ -55,8 +56,7 @@ class downloadThread : public QThread {
Q_OBJECT
private:
QStringList url_list;
QStringList downloading_list;
QQueue<QString> urls_queue;
QMutex mutex;
QWaitCondition condition;
bool abort;

Loading…
Cancel
Save