diff options
Diffstat (limited to 'client/threadboundary.h')
-rw-r--r-- | client/threadboundary.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/client/threadboundary.h b/client/threadboundary.h new file mode 100644 index 0000000..8847849 --- /dev/null +++ b/client/threadboundary.h | |||
@@ -0,0 +1,27 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include <QObject> | ||
4 | #include <functional> | ||
5 | |||
6 | namespace async { | ||
7 | /* | ||
8 | * A helper class to invoke a method in a different thread using the event loop. | ||
9 | * The ThreadBoundary object must live in the thread where the function should be called. | ||
10 | */ | ||
11 | class ThreadBoundary : public QObject { | ||
12 | Q_OBJECT | ||
13 | public: | ||
14 | ThreadBoundary(); | ||
15 | virtual ~ThreadBoundary(); | ||
16 | |||
17 | //Call in worker thread | ||
18 | void callInMainThread(std::function<void()> f) { | ||
19 | QMetaObject::invokeMethod(this, "addValueInMainThread", Qt::QueuedConnection, QGenericReturnArgument(), Q_ARG(std::function<void()>, f)); | ||
20 | } | ||
21 | public slots: | ||
22 | //Get's called in main thread by it's eventloop | ||
23 | void addValueInMainThread(std::function<void()> f) { | ||
24 | f(); | ||
25 | } | ||
26 | }; | ||
27 | } | ||