How to execute a functor or a lambda in a given thread in Qt, GCD-style?
Problem:
In ObjC with GCD, you can execute a lambda in any of the threads that spin an event loop using dispatch_sync or dispatch_async functions. It executes something (equivalent to [] { /* do sth */ } in C ) in the main thread's queue, either blocking or asynchronously. How can you do the same in Qt?
Solution:
In Qt, you can achieve a similar behavior by delivering an event that wraps the functor to a consumer object residing in the desired thread, a process known as metacall posting. Here's how you can do it:
Qt 5.10 & up TL;DR
// invoke on the main thread QMetaObject::invokeMethod(qApp, []{ ... }); // invoke on an object's thread QMetaObject::invokeMethod(obj, []{ ... }); // invoke on a particular thread QMetaObject::invokeMethod(QAbstractEventDispatcher::instance(thread), []{ ... });
TL;DR for functors Qt 5.10 & up
// https://github.com/KubaO/stackoverflown/tree/master/questions/metacall-21646467 // Qt 5.10 & up - it's all done template <typename F> static void postToObject(F &&fun, QObject *obj = qApp) { QMetaObject::invokeMethod(obj, std::forward<F>(fun)); } template <typename F> static void postToThread(F && fun, QThread *thread = qApp->thread()) { auto *obj = QAbstractEventDispatcher::instance(thread); Q_ASSERT(obj); QMetaObject::invokeMethod(obj, std::forward<F>(fun)); }
TL;DR for methods/slots Qt 5.10 & up
// Qt 5/4 template <typename T, typename R> static void postToObject(T * obj, R(T::* method)()) { struct Event : public QEvent { T * obj; R(T::* method)(); Event(T * obj, R(T::*method)()): QEvent(QEvent::None), obj(obj), method(method) {} ~Event() { (obj->*method)(); } }; if (qobject_cast<QThread*>(obj)) qWarning() << "posting a call to a thread object - this may be a bug"; QCoreApplication::postEvent(obj, new Event(obj, method)); }
Qt 5.10 & up TL;DR: What about a single shot timer?
template <typename F> static void postToObject(F && fun, QObject * obj = qApp) { if (qobject_cast<QThread*>(obj)) qWarning() << "posting a call to a thread object - consider using postToThread"; QTimer::singleShot(0, obj, std::forward<F>(fun)); }
Common Code Qt 5.10 & up
#ifndef HAS_FUNCTORCALLCONSUMER namespace FunctorCallConsumer { bool needsRunningThread() { return true; } QObject * forThread(QThread * thread) { Q_ASSERT(thread); QObject * target = thread == qApp->thread() ? static_cast<QObject*>(qApp) : QAbstractEventDispatcher::instance(thread); Q_ASSERT_X(target, "postMetaCall", "the receiver thread must have an event loop"); return target; } } #endif
Qt 4/5 Solution Using a Temporary Object as The Signal Source
#include <QtCore> #include <functional> namespace FunctorCallConsumer { QObject * forThread(QThread*); } #define HAS_POSTMETACALL void postMetaCall(QThread * thread, const std::function<void()> & fun) { QObject signalSource; QObject::connect(&signalSource, &QObject::destroyed, FunctorCallConsumer::forThread(thread), [=](QObject*){ fun(); }); } #ifdef __cpp_init_captures void postMetaCall(QThread * thread, std::function<void()> && fun) { QObject signalSource; QObject::connect(&signalSource, &QObject::destroyed, FunctorCallConsumer::forThread(thread), [fun(std::move(fun))](QObject*){ fun(); }); } #endif
Qt 4/5 Solution Using QEvent Destructor
#include <QtCore> #include <functional> class FunctorCallEvent : public QEvent { std::function<void()> m_fun; QThread * m_thread; public: FunctorCallEvent(const std::function<void()> & fun, QObject * receiver) : QEvent(QEvent::None), m_fun(fun), m_thread(receiver->thread()) {} FunctorCallEvent(std::function<void()> && fun, QObject * receiver) : QEvent(QEvent::None), m_fun(std::move(fun)), m_thread(receiver->thread()) { qDebug() << "move semantics"; } ~FunctorCallEvent() { if (QThread::currentThread() == m_thread) m_fun(); else qWarning() << "Dropping a functor call destined for thread" << m_thread; } };
Qt 5 Solution Using the Private QMetaCallEvent
#include <QtCore> #include <private/qobject_p.h> #include <functional> class FunctorCallEvent : public QMetaCallEvent { public: template <typename Functor> FunctorCallEvent(Functor && fun, QObject * receiver) : QMetaCallEvent(new QtPrivate::QFunctorSlotObject<Functor, 0, typename QtPrivate::List_Left<void, 0>::Value, void> (std::forward<Functor>(fun)), receiver, 0, 0, 0, (void**)malloc(sizeof(void*))) {} };
Qt 4/5 Solution Using a Custom Event and Consumer
#include <QtCore> #include <functional> class FunctorCallEvent : public QEvent { std::function<void()> m_fun; public: FunctorCallEvent(const std::function<void()> & fun, QObject *) : QEvent(QEvent::None), m_fun(fun) {} FunctorCallEvent(std::function<void()> && fun, QObject *) : QEvent(QEvent::None), m_fun(std::move(fun)) { qDebug() << "move semantics"; } void call() { m_fun(); } }; #define HAS_FUNCTORCALLCONSUMER class FunctorCallConsumer : public QObject { typedef QMap<QThread*, FunctorCallConsumer*> Map; static QObject * m_appThreadObject; static QMutex m_threadObjectMutex; static Map m_threadObjects; bool event(QEvent * ev) { if (!dynamic_cast<FunctorCallEvent*>(ev)) return QObject::event(ev); static_cast<FunctorCallEvent*>(ev)->call(); return true; } };
The above is the detailed content of How to Execute Functors or Lambdas in Specific Qt Threads?. For more information, please follow other related articles on the PHP Chinese website!