Refactored out the worker-thread paradigm thing we use

Into a pure mixin class called "ThreadObjectMixin".

TODO: make the TcpServer also use this mixin.
This commit is contained in:
Calin Culianu 2019-04-28 10:27:17 +03:00
parent ab4483018c
commit 9fc0343b45
8 changed files with 78 additions and 89 deletions

49
ThreadObjectMixin.cpp Normal file
View file

@ -0,0 +1,49 @@
#include "ThreadObjectMixin.h"
ThreadObjectMixin::ThreadObjectMixin()
{
origThread = QThread::currentThread();
origThread->connect(origThread, &QThread::finished, [this]{
Warning() << "ThreadObjectMixin " << qobj()->objectName() << ": original thread ended! Settings original thread to main thread! FIXME!";
origThread = qApp->thread();
});
}
ThreadObjectMixin::~ThreadObjectMixin()
{
stop(); // this is here for paranoia reasons. In normal app function derived class should make sure it's stopped before we get here. This should be a NO-OP!
}
void ThreadObjectMixin::start()
{
if (_thread.isRunning()) return;
Debug() << qobj()->objectName() << " starting thread";
chan.clear();
qobj()->moveToThread(&_thread);
conns.push_back(qobj()->connect(&_thread, &QThread::started, qobj(), [this](){on_started();}));
conns.push_back(qobj()->connect(&_thread, &QThread::finished, qobj(), [this](){on_finished();}));
_thread.start();
}
void ThreadObjectMixin::stop()
{
if (_thread.isRunning()) {
Debug() << qobj()->objectName() << " thread is running, joining thread";
_thread.quit();
_thread.wait();
}
for (auto c : conns) {
qobj()->disconnect(c);
}
conns.clear();
}
void ThreadObjectMixin::on_started()
{
}
void ThreadObjectMixin::on_finished()
{
qobj()->moveToThread(origThread);
}