Browse Source

deleteThread and downloadThread threads are handled better and destructed in a cleaner way

adaptive-webui-19844
Christophe Dumez 18 years ago
parent
commit
825c874718
  1. 4
      src/deleteThread.h
  2. 30
      src/downloadThread.h

4
src/deleteThread.h

@ -37,6 +37,10 @@ class deleteThread : public QThread {
start(); start();
} }
~deleteThread(){
wait();
}
signals: signals:
void deletionFinished(deleteThread*) const; void deletionFinished(deleteThread*) const;

30
src/downloadThread.h

@ -40,27 +40,44 @@ class downloadThread : public QThread {
QStringList url_list; QStringList url_list;
QMutex mutex; QMutex mutex;
QWaitCondition condition; QWaitCondition condition;
bool abort;
signals: signals:
void downloadFinished(const QString& url, const QString& file_path, int return_code, const QString& errorBuffer); void downloadFinished(const QString& url, const QString& file_path, int return_code, const QString& errorBuffer);
public: public:
downloadThread(QObject* parent) : QThread(parent){} downloadThread(QObject* parent) : QThread(parent){
mutex.lock();
abort = false;
mutex.unlock();
}
void downloadUrl(const QString& url){ ~downloadThread(){
mutex.lock(); mutex.lock();
abort = true;
condition.wakeOne();
mutex.unlock();
wait();
}
void downloadUrl(const QString& url){
QMutexLocker locker(&mutex);
qDebug("In Download thread function, mutex locked"); qDebug("In Download thread function, mutex locked");
url_list << url; url_list << url;
mutex.unlock();
qDebug("In Download thread function, mutex unlocked (url added)"); qDebug("In Download thread function, mutex unlocked (url added)");
if(!isRunning()){ if(!isRunning()){
qDebug("In Download thread function, Launching thread (was stopped)"); qDebug("In Download thread function, Launching thread (was stopped)");
start(); start();
}else{
condition.wakeOne();
} }
} }
protected:
void run(){ void run(){
forever{ forever{
if(abort)
return;
mutex.lock(); mutex.lock();
qDebug("In Download thread RUN, mutex locked"); qDebug("In Download thread RUN, mutex locked");
if(url_list.size() != 0){ if(url_list.size() != 0){
@ -76,6 +93,8 @@ class downloadThread : public QThread {
filePath = tmpfile->fileName(); filePath = tmpfile->fileName();
} }
delete tmpfile; delete tmpfile;
if(abort)
return;
FILE *file = fopen((const char*)filePath.toUtf8(), "w"); FILE *file = fopen((const char*)filePath.toUtf8(), "w");
if(!file){ if(!file){
std::cerr << "Error: could not open temporary file...\n"; std::cerr << "Error: could not open temporary file...\n";
@ -140,9 +159,10 @@ class downloadThread : public QThread {
emit downloadFinished(url, filePath, return_code, QString(errorBuffer)); emit downloadFinished(url, filePath, return_code, QString(errorBuffer));
qDebug("In Download thread RUN, signal emitted, ErrorBuffer: %s", errorBuffer); qDebug("In Download thread RUN, signal emitted, ErrorBuffer: %s", errorBuffer);
}else{ }else{
qDebug("In Download thread RUN, mutex still locked (no urls) -> sleeping");
condition.wait(&mutex);
mutex.unlock(); mutex.unlock();
qDebug("In Download thread RUN, mutex unlocked (no urls) -> stopping"); qDebug("In Download thread RUN, woke up, mutex unlocked");
break;
} }
} }
} }

Loading…
Cancel
Save