From e5125013a2661b30f21323f1a3f925103e8d589f Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 22 Dec 2015 09:25:18 +0100 Subject: Threadboundary cleanup --- common/resultprovider.h | 4 +--- common/threadboundary.cpp | 22 +++++++++++++++++++++- common/threadboundary.h | 36 +++++++++++++++++------------------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/common/resultprovider.h b/common/resultprovider.h index 2c373c3..7f090e9 100644 --- a/common/resultprovider.h +++ b/common/resultprovider.h @@ -97,9 +97,7 @@ private: //That way the result emitter implementation doesn't have to care about threadsafety at all. //The alternative would be to make all handlers of the emitter threadsafe. if (auto emitter = mResultEmitter.toStrongRef()) { - emitter->mThreadBoundary.callInMainThread([f]() { - f(); - }); + emitter->mThreadBoundary.callInMainThread(f); } } diff --git a/common/threadboundary.cpp b/common/threadboundary.cpp index 48fd11a..238f5b4 100644 --- a/common/threadboundary.cpp +++ b/common/threadboundary.cpp @@ -23,10 +23,30 @@ Q_DECLARE_METATYPE(std::function); namespace async { -ThreadBoundary::ThreadBoundary(): QObject() { qRegisterMetaType >("std::function"); } +ThreadBoundary::ThreadBoundary() + : QObject() +{ + qRegisterMetaType >("std::function"); +} + ThreadBoundary:: ~ThreadBoundary() { } +void ThreadBoundary::callInMainThread(std::function f) +{ + /* + * This implementation causes lambdas to pile up if the target thread is the same as the caller thread, or the caller thread calls faster + * than the target thread is able to execute the function calls. In that case any captures will equally pile up, resulting + * in significant memory usage i.e. due to Emitter::addHandler calls that each capture a domain object. + */ + QMetaObject::invokeMethod(this, "runInMainThread", Qt::QueuedConnection, QGenericReturnArgument(), Q_ARG(std::function, f)); +} + +void ThreadBoundary::runInMainThread(std::function f) +{ + f(); +} + } diff --git a/common/threadboundary.h b/common/threadboundary.h index d73bdae..0d8ed3b 100644 --- a/common/threadboundary.h +++ b/common/threadboundary.h @@ -24,24 +24,22 @@ #include namespace async { - /* - * A helper class to invoke a method in a different thread using the event loop. - * The ThreadBoundary object must live in the thread where the function should be called. - */ - class ThreadBoundary : public QObject { - Q_OBJECT - public: - ThreadBoundary(); - virtual ~ThreadBoundary(); - //Call in worker thread - void callInMainThread(std::function f) { - QMetaObject::invokeMethod(this, "runInMainThread", Qt::QueuedConnection, QGenericReturnArgument(), Q_ARG(std::function, f)); - } - public slots: - //Get's called in main thread by it's eventloop - void runInMainThread(std::function f) { - f(); - } - }; +/* +* A helper class to invoke a method in a different thread using the event loop. +* The ThreadBoundary object must live in the thread where the function should be called. +*/ +class ThreadBoundary : public QObject { + Q_OBJECT +public: + ThreadBoundary(); + virtual ~ThreadBoundary(); + + //Call in worker thread + void callInMainThread(std::function f); +public slots: + //Get's called in main thread by it's eventloop + void runInMainThread(std::function f); +}; + } -- cgit v1.2.3