Gracefully shut down the worker

This commit is contained in:
ArsenArsen 2017-05-30 15:51:25 +02:00
parent 3a388aa43e
commit 869c2d0031
3 changed files with 9 additions and 5 deletions

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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