diff options
-rw-r--r-- | common/resultprovider.h | 4 | ||||
-rw-r--r-- | common/threadboundary.cpp | 22 | ||||
-rw-r--r-- | 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: | |||
97 | //That way the result emitter implementation doesn't have to care about threadsafety at all. | 97 | //That way the result emitter implementation doesn't have to care about threadsafety at all. |
98 | //The alternative would be to make all handlers of the emitter threadsafe. | 98 | //The alternative would be to make all handlers of the emitter threadsafe. |
99 | if (auto emitter = mResultEmitter.toStrongRef()) { | 99 | if (auto emitter = mResultEmitter.toStrongRef()) { |
100 | emitter->mThreadBoundary.callInMainThread([f]() { | 100 | emitter->mThreadBoundary.callInMainThread(f); |
101 | f(); | ||
102 | }); | ||
103 | } | 101 | } |
104 | } | 102 | } |
105 | 103 | ||
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 @@ | |||
23 | Q_DECLARE_METATYPE(std::function<void()>); | 23 | Q_DECLARE_METATYPE(std::function<void()>); |
24 | 24 | ||
25 | namespace async { | 25 | namespace async { |
26 | ThreadBoundary::ThreadBoundary(): QObject() { qRegisterMetaType<std::function<void()> >("std::function<void()>"); } | 26 | ThreadBoundary::ThreadBoundary() |
27 | : QObject() | ||
28 | { | ||
29 | qRegisterMetaType<std::function<void()> >("std::function<void()>"); | ||
30 | } | ||
31 | |||
27 | ThreadBoundary:: ~ThreadBoundary() | 32 | ThreadBoundary:: ~ThreadBoundary() |
28 | { | 33 | { |
29 | } | 34 | } |
30 | 35 | ||
36 | void ThreadBoundary::callInMainThread(std::function<void()> f) | ||
37 | { | ||
38 | /* | ||
39 | * This implementation causes lambdas to pile up if the target thread is the same as the caller thread, or the caller thread calls faster | ||
40 | * than the target thread is able to execute the function calls. In that case any captures will equally pile up, resulting | ||
41 | * in significant memory usage i.e. due to Emitter::addHandler calls that each capture a domain object. | ||
42 | */ | ||
43 | QMetaObject::invokeMethod(this, "runInMainThread", Qt::QueuedConnection, QGenericReturnArgument(), Q_ARG(std::function<void()>, f)); | ||
44 | } | ||
45 | |||
46 | void ThreadBoundary::runInMainThread(std::function<void()> f) | ||
47 | { | ||
48 | f(); | ||
49 | } | ||
50 | |||
31 | } | 51 | } |
32 | 52 | ||
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 @@ | |||
24 | #include <functional> | 24 | #include <functional> |
25 | 25 | ||
26 | namespace async { | 26 | namespace async { |
27 | /* | ||
28 | * A helper class to invoke a method in a different thread using the event loop. | ||
29 | * The ThreadBoundary object must live in the thread where the function should be called. | ||
30 | */ | ||
31 | class ThreadBoundary : public QObject { | ||
32 | Q_OBJECT | ||
33 | public: | ||
34 | ThreadBoundary(); | ||
35 | virtual ~ThreadBoundary(); | ||
36 | 27 | ||
37 | //Call in worker thread | 28 | /* |
38 | void callInMainThread(std::function<void()> f) { | 29 | * A helper class to invoke a method in a different thread using the event loop. |
39 | QMetaObject::invokeMethod(this, "runInMainThread", Qt::QueuedConnection, QGenericReturnArgument(), Q_ARG(std::function<void()>, f)); | 30 | * The ThreadBoundary object must live in the thread where the function should be called. |
40 | } | 31 | */ |
41 | public slots: | 32 | class ThreadBoundary : public QObject { |
42 | //Get's called in main thread by it's eventloop | 33 | Q_OBJECT |
43 | void runInMainThread(std::function<void()> f) { | 34 | public: |
44 | f(); | 35 | ThreadBoundary(); |
45 | } | 36 | virtual ~ThreadBoundary(); |
46 | }; | 37 | |
38 | //Call in worker thread | ||
39 | void callInMainThread(std::function<void()> f); | ||
40 | public slots: | ||
41 | //Get's called in main thread by it's eventloop | ||
42 | void runInMainThread(std::function<void()> f); | ||
43 | }; | ||
44 | |||
47 | } | 45 | } |