Gracefully shut down the worker
This commit is contained in:
parent
3a388aa43e
commit
869c2d0031
1
main.cpp
1
main.cpp
@ -60,6 +60,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
MainWindow w;
|
MainWindow w;
|
||||||
Worker::init();
|
Worker::init();
|
||||||
|
a.connect(&a, &QApplication::aboutToQuit, Worker::end);
|
||||||
if (!parser.isSet(h)) w.show();
|
if (!parser.isSet(h)) w.show();
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
@ -30,18 +30,20 @@ Worker::Worker() : QObject() {
|
|||||||
moveToThread(thr);
|
moveToThread(thr);
|
||||||
connect(thr, &QThread::started, this, &Worker::process);
|
connect(thr, &QThread::started, this, &Worker::process);
|
||||||
connect(thr, &QThread::finished, thr, &QThread::deleteLater);
|
connect(thr, &QThread::finished, thr, &QThread::deleteLater);
|
||||||
connect(this, &Worker::finished, thr, &QThread::quit);
|
|
||||||
connect(this, &Worker::finished, this, &Worker::deleteLater);
|
connect(this, &Worker::finished, this, &Worker::deleteLater);
|
||||||
connect(thr, &QThread::finished, thr, &QThread::deleteLater);
|
connect(thr, &QThread::finished, thr, &QThread::deleteLater);
|
||||||
thr->start();
|
thr->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
Worker::~Worker() {
|
Worker::~Worker() {
|
||||||
end();
|
_end();
|
||||||
thread()->wait();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Worker::end() {
|
void Worker::end() {
|
||||||
|
inst->_end();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Worker::_end() {
|
||||||
QMutexLocker ml(&endLock);
|
QMutexLocker ml(&endLock);
|
||||||
_ended = true;
|
_ended = true;
|
||||||
}
|
}
|
||||||
@ -59,7 +61,7 @@ void Worker::process() {
|
|||||||
c->consumer(c->image.convertToFormat(c->targetFormat));
|
c->consumer(c->image.convertToFormat(c->targetFormat));
|
||||||
}
|
}
|
||||||
lock.unlock();
|
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();
|
emit finished();
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,9 @@ private:
|
|||||||
QThread *thr;
|
QThread *thr;
|
||||||
QQueue<_WorkerContext *> qqueue; // Say that ten times as fast
|
QQueue<_WorkerContext *> qqueue; // Say that ten times as fast
|
||||||
bool _ended;
|
bool _ended;
|
||||||
|
void _end();
|
||||||
|
|
||||||
void _queue(WorkerContext *context);
|
void _queue(WorkerContext *context);
|
||||||
void end();
|
|
||||||
bool ended();
|
bool ended();
|
||||||
|
|
||||||
static Worker *inst;
|
static Worker *inst;
|
||||||
@ -48,6 +48,7 @@ signals:
|
|||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void process();
|
void process();
|
||||||
|
static void end();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // WORKER_HPP
|
#endif // WORKER_HPP
|
||||||
|
Loading…
Reference in New Issue
Block a user