如何在 Qt、GCD 风格的给定线程中执行函子或 lambda?
问题:
在具有 GCD 的 ObjC 中,您可以在任何使用dispatch_sync或dispatch_async函数旋转事件循环的线程。它在主线程队列中执行某些操作(相当于 C 中的 [] { /* do sth */ } ),可以是阻塞的,也可以是异步的。如何在 Qt 中执行相同的操作?
解决方案:
在 Qt 中,您可以通过传递将仿函数包装到消费者对象的事件来实现类似的行为驻留在所需的线程中,这个过程称为元调用发布。具体操作方法如下:
Qt 5.10 及更高版本 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 用于函子 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 用于 Qt 5.10 及以上版本的方法/槽
// 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 及以上 TL;DR:单拍怎么办计时器?
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)); }
通用代码 Qt 5.10 及更高版本
#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 解决方案来源
#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
使用 QEvent 析构函数的 Qt 4/5 解决方案
#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; } };
使用私有 QMetaCallEvent 的 Qt 5 解决方案
#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 使用自定义事件和消费者的解决方案
#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; } };
以上是如何在特定的 Qt 线程中执行函子或 Lambda?的详细内容。更多信息请关注PHP中文网其他相关文章!