如何在Qt,GCD风格的给定线程中执行仿函数或lambda?

Alb*_*ert 35 c++ qt multithreading

在带有GCD的ObjC中,有一种在旋转事件循环的任何线程中执行lambda的方法.例如:

dispatch_sync(dispatch_get_main_queue(), ^{ /* do sth */ });
Run Code Online (Sandbox Code Playgroud)

要么:

dispatch_async(dispatch_get_main_queue(), ^{ /* do sth */ });
Run Code Online (Sandbox Code Playgroud)

[]{ /* do sth */ }在主线程的队列中执行某些操作(相当于在C++中),阻塞或异步.

我怎么能在Qt中做同样的事情?

从我所读到的,我想解决方案将以某种方式向主线程的某个对象发送信号.但是什么对象呢?只是QApplication::instance()?(那是那时生活在主线程中的唯一对象.)什么信号?


从目前的答案和我目前的研究来看,似乎我需要一些虚拟对象来坐在主线程中,有一些插槽只是等待进入某些代码来执行.

所以,我决定使用子类QApplication来添加它.我目前的代码,但不起作用(但也许你可以帮忙):

#include <QApplication>
#include <QThread>
#include <QMetaMethod>
#include <functional>
#include <assert.h>

class App : public QApplication
{
    Q_OBJECT

public:
    App();

signals:

public slots:
    void genericExec(std::function<void(void)> func) {
        func();
    }

private:
    // cache this
    QMetaMethod genericExec_method;
public:
    void invokeGenericExec(std::function<void(void)> func, Qt::ConnectionType connType) {
        if(!genericExec_method) {
            QByteArray normalizedSignature = QMetaObject::normalizedSignature("genericExec(std::function<void(void)>)");
            int methodIndex = this->metaObject()->indexOfSlot(normalizedSignature);
            assert(methodIndex >= 0);
            genericExec_method = this->metaObject()->method(methodIndex);
        }
        genericExec_method.invoke(this, connType, Q_ARG(std::function<void(void)>, func));
    }

};

static inline
void execInMainThread_sync(std::function<void(void)> func) {
    if(qApp->thread() == QThread::currentThread())
        func();
    else {
        ((App*) qApp)->invokeGenericExec(func, Qt::BlockingQueuedConnection);
    }
}

static inline
void execInMainThread_async(std::function<void(void)> func) {
    ((App*) qApp)->invokeGenericExec(func, Qt::QueuedConnection);
}
Run Code Online (Sandbox Code Playgroud)

Rei*_*ica 77

这当然是可能的.任何解决方案都将集中于提供将仿函数包装到驻留在所需线程中的使用者对象的事件.我们将此操作称为metacall发布.细节可以以多种方式执行.

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),
                         []{ ... });
Run Code Online (Sandbox Code Playgroud)

TL;用于仿函数的DR

// 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));
}

// Qt 5/4 - preferred, has least allocations

namespace detail {
template <typename F>
struct FEvent : public QEvent {
   using Fun = typename std::decay<F>::type;
   Fun fun;
   FEvent(Fun && fun) : QEvent(QEvent::None), fun(std::move(fun)) {}
   FEvent(const Fun & fun) : QEvent(QEvent::None), fun(fun) {}
   ~FEvent() { fun(); }
}; }

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";
   QCoreApplication::postEvent(obj, new detail::FEvent<F>(std::forward<F>(fun)));
}

template <typename F>
static void postToThread(F && fun, QThread * thread = qApp->thread()) {
   QObject * obj = QAbstractEventDispatcher::instance(thread);
   Q_ASSERT(obj);
   QCoreApplication::postEvent(obj, new detail::FEvent<F>(std::forward<F>(fun)));
}
Run Code Online (Sandbox Code Playgroud)
// Qt 5 - alternative version

template <typename F>
static void postToObject2(F && fun, QObject * obj = qApp) {
   if (qobject_cast<QThread*>(obj))
      qWarning() << "posting a call to a thread object - consider using postToThread";
   QObject src;
   QObject::connect(&src, &QObject::destroyed, obj, std::forward<F>(fun),
                    Qt::QueuedConnection);
}

template <typename F>
static void postToThread2(F && fun, QThread * thread = qApp->thread()) {
   QObject * obj = QAbstractEventDispatcher::instance(thread);
   Q_ASSERT(obj);
   QObject src;
   QObject::connect(&src, &QObject::destroyed, obj, std::forward<F>(fun),
                    Qt::QueuedConnection);
}
Run Code Online (Sandbox Code Playgroud)
void test1() {
   QThread t;
   QObject o;
   o.moveToThread(&t);

   // Execute in given object's thread
   postToObject([&]{ o.setObjectName("hello"); }, &o);
   // or
   postToObject(std::bind(&QObject::setObjectName, &o, "hello"), &o);

   // Execute in given thread
   postToThread([]{ qDebug() << "hello from worker thread"; });

   // Execute in the main thread
   postToThread([]{ qDebug() << "hello from main thread"; });
}
Run Code Online (Sandbox Code Playgroud)

TL;方法/插槽的DR

// 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));
}

void test2() {
   QThread t;
   struct MyObject : QObject { void method() {} } obj;
   obj.moveToThread(&t);

   // Execute in obj's thread
   postToObject(&obj, &MyObject::method);
}
Run Code Online (Sandbox Code Playgroud)

TL; DR:单发计时器怎么样?

所有上述方法都适用于没有事件循环的线程.由于QTBUG-66458,QTimer::singleShot在源线程中也需要一个事件循环.然后postToObject变得非常简单,你可以QTimer::singleShot直接使用,虽然这是一个尴尬的名字,隐藏了那些不熟悉这个成语的人的意图.即使您不需要类型检查,通过名为更好地指示意图的函数的间接也是有意义的:

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));
}
Run Code Online (Sandbox Code Playgroud)

共同准则

让我们根据以下常见代码定义我们的问题.最简单的解决方案将事件发布到应用程序对象,iff目标线程是主线程,或者发布到任何其他给定线程的事件调度程序.由于事件调度程序仅在QThread::run输入后才存在,因此我们通过返回true来指示线程运行的要求needsRunningThread.

#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
Run Code Online (Sandbox Code Playgroud)

metacall发布函数以其最简单的形式,需要函子调用使用者为给定线程提供对象,并实例化仿函数调用事件.事件的实现仍然领先于我们,并且是各种实现之间的本质区别.

第二个重载采用了仿函数的右值引用,可能会在仿函数上保存复制操作.如果continuation包含复制昂贵的数据,这将非常有用.

#ifndef HAS_POSTMETACALL
void postMetaCall(QThread * thread, const std::function<void()> & fun) {
   auto receiver = FunctorCallConsumer::forThread(thread);
   QCoreApplication::postEvent(receiver, new FunctorCallEvent(fun, receiver));
}

void postMetaCall(QThread * thread, std::function<void()> && fun) {
   auto receiver = FunctorCallConsumer::forThread(thread);
   QCoreApplication::postEvent(receiver,
                               new FunctorCallEvent(std::move(fun), receiver));
}
#endif
Run Code Online (Sandbox Code Playgroud)

出于演示目的,工作线程首先将metacall发布到主线程,然后推迟QThread::run()启动事件循环以侦听来自其他线程的可能的metacall.如果消费者的实现需要,互斥体用于允许线程用户以简单的方式等待线程启动.对于上面给出的默认事件使用者,这种等待是必要的.

class Worker : public QThread {
   QMutex m_started;
   void run() {
      m_started.unlock();
      postMetaCall(qApp->thread(), []{
         qDebug() << "worker functor executes in thread" << QThread::currentThread();
      });
      QThread::run();
   }
public:
   Worker(QObject * parent = 0) : QThread(parent) { m_started.lock(); }
   ~Worker() { quit(); wait(); }
   void waitForStart() { m_started.lock(); m_started.unlock(); }
};
Run Code Online (Sandbox Code Playgroud)

最后,我们启动上述工作线程,将metacall发布到主(应用程序)线程,应用程序线程将metacall发布到工作线程.

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);
   a.thread()->setObjectName("main");
   Worker worker;
   worker.setObjectName("worker");
   qDebug() << "worker thread:" << &worker;
   qDebug() << "main thread:" << QThread::currentThread();
   if (FunctorCallConsumer::needsRunningThread()) {
      worker.start();
      worker.waitForStart();
   }
   postMetaCall(&worker, []{ qDebug() << "main functor executes in thread" << QThread::currentThread(); });
   if (!FunctorCallConsumer::needsRunningThread()) worker.start();
   QMetaObject::invokeMethod(&a, "quit", Qt::QueuedConnection);
   return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

在所有实现中,输出看起来大致如下.函子穿过线程:在主线程中创建的线程在工作线程中执行,反之亦然.

worker thread: QThread(0x7fff5692fc20, name = "worker") 
main thread: QThread(0x7f86abc02f00, name = "main") 
main functor executes in thread QThread(0x7fff5692fc20, name = "worker") 
worker functor executes in thread QThread(0x7f86abc02f00, name = "main") 
Run Code Online (Sandbox Code Playgroud)

Qt 5解决方案使用临时对象作为信号源

Qt 5最简单的方法是使用临时QObject作为信号源,并将仿函数连接到其destroyed(QObject*)信号.当postMetaCall返回时,signalSource被破坏,其发出destroyed的信号,并过帐metacall到代理对象.

这可能是C++ 11风格中最简洁直接的实现.该signalSource对象以C++ 11 RAII方式用于其破坏的副作用.短语"副作用"在C++ 11的语义中具有含义,不应被解释为"不可靠"或"不受欢迎" - 它只不过是.QObject与我们的合同是destroyed在执行其析构函数期间的某些时间.我们非常欢迎使用这一事实.

#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
// Common Code follows here
Run Code Online (Sandbox Code Playgroud)

如果我们只打算发布到主线程,代码几乎变得微不足道:

void postToMainThread(const std::function<void()> & fun) {
  QObject signalSource;
  QObject::connect(&signalSource, &QObject::destroyed, qApp, [=](QObject*){
    fun();
  });
}

#ifdef __cpp_init_captures
void postToMainThread(std::function<void()> && fun) {
  QObject signalSource;
  QObject::connect(&signalSource, &QObject::destroyed, qApp, [fun(std::move(fun))](QObject*){
    fun();
  });
}
#endif
Run Code Online (Sandbox Code Playgroud)

Qt 4/5解决方案使用QEvent析构函数

可以直接应用相同的方法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;
   }
};
// Common Code follows here
Run Code Online (Sandbox Code Playgroud)

要仅发布到主线程,事情变得更简单:

class FunctorCallEvent : public QEvent {
   std::function<void()> m_fun;
public:
   FunctorCallEvent(const std::function<void()> & fun) :
      QEvent(QEvent::None), m_fun(fun) {}
   FunctorCallEvent(std::function<void()> && fun, QObject * receiver) :
      QEvent(QEvent::None), m_fun(std::move(fun)) {}
   ~FunctorCallEvent() {
      m_fun();
   }
};

void postToMainThread(const std::function<void()> & fun) {
   QCoreApplication::postEvent(qApp, new FunctorCallEvent(fun);
}

void postToMainThread(std::function<void()> && fun) {
   QCoreApplication::postEvent(qApp, new FunctorCallEvent(std::move(fun)));
}
Run Code Online (Sandbox Code Playgroud)

Qt 5解决方案使用私有QMetaCallEvent

仿函数可以包装在Qt 5槽对象的有效载荷中QMetaCallEvent.函数将被调用QObject::event,因此可以发布到目标线程中的任何对象.此解决方案使用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*))) {}
   // Metacalls with slot objects require an argument array for the return type, even if it's void.
};
// Common Code follows here
Run Code Online (Sandbox Code Playgroud)

Qt 4/5解决方案使用自定义事件和消费者

我们重新实现event()对象的方法,并让它调用functor.这要求在仿函数发布到的每个线程中使用显式的事件使用者对象.对象在其线程完成时清除,或者对于主线程,在应用程序实例被销毁时清除.它适用于Qt 4和Qt 5.使用rvalue引用可避免复制临时仿函数.

#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;
   }
   FunctorCallConsumer() {}
   ~FunctorCallConsumer() {
      qDebug() << "consumer done for thread" << thread();
      Q_ASSERT(thread());
      QMutexLocker lock(&m_threadObjectMutex);
      m_threadObjects.remove(thread());
   }
   static void deleteAppThreadObject() {
      delete m_appThreadObject;
      m_appThreadObject = nullptr;
   }
public:
   static bool needsRunningThread() { return false; }
   static FunctorCallConsumer * forThread(QThread * thread) {
      QMutexLocker lock(&m_threadObjectMutex);
      Map map = m_threadObjects;
      lock.unlock();
      Map::const_iterator it = map.find(thread);
      if (it != map.end()) return *it;
      FunctorCallConsumer * consumer = new FunctorCallConsumer;
      consumer->moveToThread(thread);
      if (thread != qApp->thread())
         QObject::connect(thread, SIGNAL(finished()), consumer, SLOT(deleteLater()));
      lock.relock();
      it = m_threadObjects.find(thread);
      if (it == m_threadObjects.end()) {
         if (thread == qApp->thread()) {
            Q_ASSERT(! m_appThreadObject);
            m_appThreadObject = consumer;
            qAddPostRoutine(&deleteAppThreadObject);
         }
         m_threadObjects.insert(thread, consumer);
         return consumer;
      } else {
         delete consumer;
         return *it;
      }
   }
};

QObject * FunctorCallConsumer::m_appThreadObject = nullptr;
QMutex FunctorCallConsumer::m_threadObjectMutex;
FunctorCallConsumer::Map FunctorCallConsumer::m_threadObjects;
// Common Code follows here
Run Code Online (Sandbox Code Playgroud)


ale*_*ria 12

可能这样的东西有用吗?

template <typename Func>
inline static void MyRunLater(Func func) {
    QTimer *t = new QTimer();
    t->moveToThread(qApp->thread());
    t->setSingleShot(true);
    QObject::connect(t, &QTimer::timeout, [=]() {
        func();
        t->deleteLater();
    });
    QMetaObject::invokeMethod(t, "start", Qt::QueuedConnection, Q_ARG(int, 0));
}
Run Code Online (Sandbox Code Playgroud)

这段代码将使lambda在主线程事件循环中尽快运行.没有args支持,这是一个非常基本的代码.

注意:我没有正确测试它.

  • 完美的工作.不需要模板化.只需要使用std :: function <void()>并让lambda创建者处理参数捕获等. (2认同)

Yur*_*yma 12

有一种新方法是我认为最简单的方法.它来自Qt 5.4.链接到文档

void QTimer::singleShot(int msec, const QObject *context, Functor functor)
Run Code Online (Sandbox Code Playgroud)

例:

QTimer::singleShot(0, qApp, []()
{
    qDebug() << "hi from event loop";
});
Run Code Online (Sandbox Code Playgroud)

lambda将在qApp线程(主线程)中执行.您可以将上下文替换为您想要的任何QObject.

更新

QTimer需要事件循环才能工作.对于没有qt事件循环的线程(std :: thread),我们可以创建一个.在std :: thread中运行lambda的代码.

QEventLoop loop;
Q_UNUSED(loop)
QTimer::singleShot(0, qApp, []()
{
    qDebug() << "singleShot from std thread";
});
Run Code Online (Sandbox Code Playgroud)

完整的例子

#include <QCoreApplication>
#include <QTimer>
#include <QDebug>
#include <thread>
#include <QThread>
#include <QEventLoop>
#include <QThread>
using std::thread;

class TestObj
        :public QObject
{
// Used new connect syntax no need for Q_OBJECT define
// you SHOULD use it. I used just to upload one file
//Q_OBJECT
public slots:
    void doWork()
    {
        qDebug() << "QThread id" << QThread::currentThreadId();
        QTimer::singleShot(0, qApp, []()
        {
            qDebug() << "singleShot from QThread" << QThread::currentThreadId();
        });
    }
};


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug() << "main thread id" << QThread::currentThreadId();

    thread testThread([]()
    {
        QEventLoop loop;
        Q_UNUSED(loop)
        qDebug() << "std::thread id" << QThread::currentThreadId();

        QTimer::singleShot(0, qApp, []()
        {
            qDebug() << "singleShot from std thread" << QThread::currentThreadId();
        });
        qDebug() << "std::thread finished";
    });
    testThread.detach();

    QThread testQThread;
    TestObj testObj;
    testObj.moveToThread(&testQThread);
    QObject::connect(&testQThread, &QThread::started, &testObj, &TestObj::doWork);
    testQThread.start();

    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)