From b89e685212fe50b66c8a156761bf89784d6460f1 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sat, 13 Apr 2019 21:52:53 +0500 Subject: [PATCH] UI: WorkerManager: Wait workers finishing. --- src/ui/util/worker/workermanager.cpp | 23 +++++++++++++---------- src/ui/util/worker/workermanager.h | 1 - src/ui/util/worker/workerobject.cpp | 4 +--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/ui/util/worker/workermanager.cpp b/src/ui/util/worker/workermanager.cpp index 9e627b21..4c4c5dbf 100644 --- a/src/ui/util/worker/workermanager.cpp +++ b/src/ui/util/worker/workermanager.cpp @@ -40,6 +40,10 @@ void WorkerManager::workerFinished(WorkerObject *worker) QMutexLocker locker(&m_mutex); m_workers.removeOne(worker); + + if (m_workers.isEmpty()) { + m_waitCondition.wakeOne(); + } } void WorkerManager::clear() @@ -53,20 +57,19 @@ void WorkerManager::abort() { QMutexLocker locker(&m_mutex); - abortWorkers(); - m_aborted = true; - m_waitCondition.wakeAll(); -} + if (!m_workers.isEmpty()) { + for (WorkerObject *worker : m_workers) { + worker->abort(); + } -void WorkerManager::abortWorkers() -{ - for (WorkerObject *worker : m_workers) { - worker->abort(); + m_waitCondition.wakeAll(); + + do { + m_waitCondition.wait(&m_mutex); + } while (!m_workers.isEmpty()); } - - m_workers.clear(); } void WorkerManager::enqueueJob(const QString &job) diff --git a/src/ui/util/worker/workermanager.h b/src/ui/util/worker/workermanager.h index 09ff79a9..d4b7950b 100644 --- a/src/ui/util/worker/workermanager.h +++ b/src/ui/util/worker/workermanager.h @@ -41,7 +41,6 @@ private: void setupWorker(); void abort(); - void abortWorkers(); private: volatile bool m_aborted; diff --git a/src/ui/util/worker/workerobject.cpp b/src/ui/util/worker/workerobject.cpp index 5c83c2de..60a659e1 100644 --- a/src/ui/util/worker/workerobject.cpp +++ b/src/ui/util/worker/workerobject.cpp @@ -19,7 +19,5 @@ void WorkerObject::run() doJob(job); } - if (!aborted()) { - manager()->workerFinished(this); - } + manager()->workerFinished(this); }