gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:38:02 30 4
gpt4 key购买 nike

在带有 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);
}

最佳答案

这当然是可能的。任何解决方案都以传递一个事件为中心,该事件将仿函数包装到驻留在所需线程中的消费者对象。我们将此操作称为元调用发布。可以以多种方式执行细节。

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 用于仿函数
// 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-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));
}

通用代码

让我们根据以下通用代码来定义我们的问题。最简单的解决方案是将事件发布到应用程序对象(如果目标线程是主线程)或任何其他给定线程的事件调度程序。由于事件调度器仅在 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

元调用发布函数,以其最简单的形式,要求仿函数调用消费者为给定线程提供对象,并实例化仿函数调用事件。事件的实现还在前面,是各种实现之间的本质区别。

第二个重载接受仿函数的右值引用,可能会保存对仿函数的复制操作。如果延续包含复制成本高的数据,这将很有帮助。
#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

出于演示目的,工作线程首先向主线程发布元调用,然后遵循 QThread::run()启动一个事件循环来监听来自其他线程的可能的元调用。如果消费者的实现需要,互斥用于允许线程用户以简单的方式等待线程启动。对于上面给出的默认事件使用者,这种等待是必要的。
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(); }
};

最后,我们启动上面的工作线程,向主(应用程序)线程发布元调用,应用程序线程向工作线程发布元调用。
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)));
}

使用私有(private) QMetaCallEvent 的 Qt 5 解决方案

仿函数可以包裹在 QMetaCallEvent 的 Qt 5 插槽对象负载中。 .仿函数将由 QObject::event 调用,因此可以发布到目标线程中的任何对象。此解决方案使用 Qt 5 的私有(private)实现细节。
#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。右值引用的使用避免了临时仿函数的复制。
#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

关于c++ - 如何在 Qt、GCD 风格的给定线程中执行仿函数或 lambda?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21646467/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com