mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-19 03:45:30 +08:00
refs #334 Added class CContinuousWorker, base class for long-lived threaded worker objects. Inherits from CWorkerBase.
This commit is contained in:
committed by
Roland Winklmeier
parent
7b352c907a
commit
2a1541786a
@@ -40,4 +40,43 @@ namespace BlackMisc
|
|||||||
QMetaObject::invokeMethod(this, "deleteLater");
|
QMetaObject::invokeMethod(this, "deleteLater");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CContinuousWorker::start(QThread::Priority priority)
|
||||||
|
{
|
||||||
|
if (m_name.isEmpty()) { m_name = metaObject()->className(); }
|
||||||
|
|
||||||
|
auto *thread = new CRegularThread(m_owner);
|
||||||
|
|
||||||
|
QString ownerName = m_owner->objectName().isEmpty() ? m_owner->metaObject()->className() : m_owner->objectName();
|
||||||
|
thread->setObjectName(ownerName + ":" + m_name);
|
||||||
|
setObjectName(m_name);
|
||||||
|
|
||||||
|
moveToThread(thread);
|
||||||
|
connect(thread, &QThread::started, this, &CContinuousWorker::initialize);
|
||||||
|
connect(thread, &QThread::finished, this, &CContinuousWorker::cleanup);
|
||||||
|
connect(thread, &QThread::finished, this, &CContinuousWorker::ps_finish);
|
||||||
|
thread->start(priority);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CContinuousWorker::quit()
|
||||||
|
{
|
||||||
|
thread()->quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CContinuousWorker::quitAndWait()
|
||||||
|
{
|
||||||
|
auto *ownThread = thread();
|
||||||
|
quit();
|
||||||
|
ownThread->wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CContinuousWorker::ps_finish()
|
||||||
|
{
|
||||||
|
setFinished();
|
||||||
|
|
||||||
|
auto *ownThread = thread();
|
||||||
|
moveToThread(ownThread->thread()); // move worker back to the thread which constructed it, so there is no race on deletion
|
||||||
|
QMetaObject::invokeMethod(ownThread, "deleteLater");
|
||||||
|
QMetaObject::invokeMethod(this, "deleteLater");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -210,6 +210,49 @@ namespace BlackMisc
|
|||||||
std::function<void()> m_task;
|
std::function<void()> m_task;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Base class for a long-lived worker object which lives in its own thread.
|
||||||
|
*/
|
||||||
|
class CContinuousWorker : public CWorkerBase
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
/*!
|
||||||
|
* Constructor.
|
||||||
|
* \param owner Will be the parent of the new thread (the worker has no parent).
|
||||||
|
* \param name A name for the worker, which will be used to create a name for the thread.
|
||||||
|
*/
|
||||||
|
CContinuousWorker(QObject *owner, const QString &name = "") : m_owner(owner), m_name(name) {}
|
||||||
|
|
||||||
|
//! Starts a thread and moves the worker into it.
|
||||||
|
void start(QThread::Priority priority = QThread::InheritPriority);
|
||||||
|
|
||||||
|
//! Stops the thread the next time around its event loop.
|
||||||
|
//! The thread and the worker will then be deleted.
|
||||||
|
//! \threadsafe
|
||||||
|
void quit();
|
||||||
|
|
||||||
|
//! Calls quit() and blocks until the thread is finished.
|
||||||
|
//! \threadsafe Will deadlock if called by the worker thread.
|
||||||
|
void quitAndWait();
|
||||||
|
|
||||||
|
protected slots:
|
||||||
|
//! Called when the thread is started.
|
||||||
|
virtual void initialize() {}
|
||||||
|
|
||||||
|
//! Called when the thread is finished.
|
||||||
|
virtual void cleanup() {}
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
//! Called after cleanup().
|
||||||
|
void ps_finish();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QObject *m_owner;
|
||||||
|
QString m_name;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user