From 869c2d00314f291dbf2184251a143a9e61052a99 Mon Sep 17 00:00:00 2001 From: ArsenArsen Date: Tue, 30 May 2017 15:51:25 +0200 Subject: [PATCH] Gracefully shut down the worker --- main.cpp | 1 + worker/worker.cpp | 10 ++++++---- worker/worker.hpp | 3 ++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 0a22327..ea285d9 100644 --- a/main.cpp +++ b/main.cpp @@ -60,6 +60,7 @@ int main(int argc, char *argv[]) { MainWindow w; Worker::init(); + a.connect(&a, &QApplication::aboutToQuit, Worker::end); if (!parser.isSet(h)) w.show(); return a.exec(); } diff --git a/worker/worker.cpp b/worker/worker.cpp index 55cbf91..ed5bf3b 100644 --- a/worker/worker.cpp +++ b/worker/worker.cpp @@ -30,18 +30,20 @@ Worker::Worker() : QObject() { moveToThread(thr); connect(thr, &QThread::started, this, &Worker::process); connect(thr, &QThread::finished, thr, &QThread::deleteLater); - connect(this, &Worker::finished, thr, &QThread::quit); connect(this, &Worker::finished, this, &Worker::deleteLater); connect(thr, &QThread::finished, thr, &QThread::deleteLater); thr->start(); } Worker::~Worker() { - end(); - thread()->wait(); + _end(); } void Worker::end() { + inst->_end(); +} + +void Worker::_end() { QMutexLocker ml(&endLock); _ended = true; } @@ -59,7 +61,7 @@ void Worker::process() { c->consumer(c->image.convertToFormat(c->targetFormat)); } lock.unlock(); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); // STL likes it's scopes + std::this_thread::sleep_for(std::chrono::milliseconds(10)); // STL likes it's scopes } emit finished(); } diff --git a/worker/worker.hpp b/worker/worker.hpp index 2ac7bb5..e47a631 100644 --- a/worker/worker.hpp +++ b/worker/worker.hpp @@ -35,9 +35,9 @@ private: QThread *thr; QQueue<_WorkerContext *> qqueue; // Say that ten times as fast bool _ended; + void _end(); void _queue(WorkerContext *context); - void end(); bool ended(); static Worker *inst; @@ -48,6 +48,7 @@ signals: public slots: void process(); + static void end(); }; #endif // WORKER_HPP