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

时间:2014-02-08 13:20:22

标签: c++ multithreading qt

在使用GCD的ObjC中,有一种方法可以在任何旋转事件循环的线程中执行lambda。例如:

dispatch_sync(dispatch_get_main_queue(), ^{ /* do sth */ });

或:

dispatch_async(dispatch_get_main_queue(), ^{ /* do sth */ });

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

我怎样才能在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);
}

5 个答案:

答案 0 :(得分:70)

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

Qt 5.10&amp; 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

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

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

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

所有上述方法都适用于没有事件循环的线程。由于QTBUG-66458QTimer::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));
}

通用代码

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

#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

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

第二个重载采用了仿函数的右值引用,可能会在仿函数上保存一个复制操作。如果延续包含复制成本高昂的数据,这将非常有用。

#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

出于演示目的,工作线程首先将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(); }
};

最后,我们启动上述工作线程,将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();
}

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

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") 

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

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

这可能是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

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

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

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

同样的方法可以直接应用于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

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

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

Qt 5解决方案使用私有QMetaCallEvent

仿函数可以包装在QMetaCallEvent的Qt 5槽对象有效负载中。函数将由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

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

我们重新实现对象的event()方法,并让它调用仿函数。这要求在仿函数发布到的每个线程中使用显式的事件使用者对象。对象在其线程完成时清除,或者对于主线程,在应用程序实例被销毁时清除。它适用于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

答案 1 :(得分:10)

这样的东西可能有用吗?

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

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

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

答案 2 :(得分:10)

我认为最简单的方法有一种。 它来自Qt 5.4。 Link to documentation

void QTimer::singleShot(int msec, const QObject *context, Functor functor)

示例:

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

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

<强>更新

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

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

完整示例

#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();
}

答案 3 :(得分:0)

其他人有很好的答案。这是我的建议。而不是像这样:-

QMetaObject::invokeMethod(socketManager,"newSocket",
                          Qt::QueuedConnection,
                          Q_ARG(QString, host),
                          Q_ARG(quint16, port.toUShort()),
                          Q_ARG(QString, username),
                          Q_ARG(QString, passhash)
                          );

做类似这样的事情会更好:-

QMetaObject::invokeMethod(socketManager,[=](){
    socketManager->newSocket(host,port.toUShort(),username,passhash);
},Qt::QueuedConnection);

答案 4 :(得分:-2)

我完全不知道你在谈论什么,但我会尝试以任何方式回答它。

让我们说你有一个有插槽功能的课程

class MyClass : public QObject
{
    Q_OBJECT
public:
    MyClass() {}

public slots: 
    void MySlot() { qDebug() << "RAWR";
};

如果你想在主线程中同步运行它,你可以直接调用该函数。为了连接信号,您需要创建一个对象并将信号连接到插槽。

class MySignalClass : public QObject
{
    Q_OBJECT
public:
    MySignalClass() {}

    signalSomthign() { emit someAwesomeSignal; }

public signals: 
    void someAwesomeSignal();
};

在主线程中,您可以执行类似

的操作
MyClass slotClass;
MySignalClass signalClass;
qobject::connect(&signalClass, SIGNAL(someAwesomeSignal), &slotClass(), SLOT(MySlot)));

所以现在如果可以将多个信号连接到该槽对象,但实际上我提供的代码与正常的函数调用不会有任何不同。你将能够看到堆栈跟踪。如果将标志qobject :: queuedConneciton添加到连接,则它将在事件循环中对插槽调用进行排队。

您也可以轻松地对信号进行线程化,但这将自动成为queuedConnection

MyClass slotClass;
MySignalClass signalClass;
QThread someThread;
slotClass.moveToThread(&someThread);
qobject::connect(&signalClass, SIGNAL(someAwesomeSignal), &slotClass(), SLOT(MySlot)));

现在你基本上会有一个线程信号。如果您的信号将被线程化,那么您所要做的就是切换到signalClass.moveToThread(&amp; someThread),当信号发出时,signalClass将在主线程中运行。

如果你不想要一个对象被调用,我不确定,lamdas可能会工作。我之前使用过它们,但我认为它们仍然需要在课堂上使用。

qobject::connect(&signalClass, &slotClass::MySlot, [=]() { /* whatever */ });

虽然我对Qt5非常肯定,但你甚至可以在连接中创建一个插槽。但是一旦你使用lambdas我不知道线程如何与它们一起工作。据我所知,你需要一个对象坐在一个线程中,基本上强制从主线程调用插槽。