如果目标对象死亡,Qt::BlockingQueuedConnection发射会发生什么

What happens with Qt::BlockingQueuedConnection emission if target object dies?

本文关键字:发射 什么 BlockingQueuedConnection 如果 Qt 目标对象      更新时间:2023-10-16

当我使用invokeMethod发送方法调用时,当发送代码等待调用,但目标对象随后死亡时,会发生什么?这会在无限的等待中结束吗?或者Qt会唤醒调用者并返回false(这将是一种未记录的行为,也是我自己的最佳猜测(?

以下示例在invokeMethod等待BlockingQueuedConnection:时删除工作对象

#include <QtCore>
//a thread that can be destroyed at any time
//see http://stackoverflow.com/a/25230470
class SafeThread : public QThread{
using QThread::run;
public:
explicit SafeThread(QObject* parent= nullptr):QThread(parent){}
~SafeThread(){ quit(); wait(); }
};
//The function queues a functor to get executed in a specified worker's thread
template <typename Func>
void PostToThread(QThread* thread, Func&& f) {
//see http://stackoverflow.com/a/21653558
QObject temporaryObject;
QObject::connect(&temporaryObject, &QObject::destroyed,
thread->eventDispatcher(), std::forward<Func>(f),
Qt::QueuedConnection);
}
//a typical QObject worker that can "printName"
class Worker  : public QObject {
Q_OBJECT
public:
using QObject::QObject;
~Worker() {
qInfo() << "destroying " << objectName()
<< " in " << QThread::currentThread()->objectName();
}
Q_SLOT void printName() {
qInfo() << "my name is " << objectName()
<< " in " << QThread::currentThread()->objectName();
}
};
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
//create worker
Worker *worker = new Worker;
worker->setObjectName("worker");
//start worker thread and move worker to it
SafeThread t;
worker->moveToThread(&t);
t.start();
//set thread names (for better output)
QThread::currentThread()->setObjectName("main_thread");
t.setObjectName("worker_thread");
//normal QMetaObject::invokeMethod usage
if(QMetaObject::invokeMethod(worker, "printName",
Qt::BlockingQueuedConnection)) {
qInfo() << "printName called successfully before deletion";
}
//the lambda function will be executed in the worker thread
PostToThread(&t, [worker]{
qInfo() << "blocking " << QThread::currentThread()->objectName();
QThread::sleep(2); //block worker thread for 2 seconds
delete worker; //delete worker
});
//at this point the worker thread is about to destroy the worker object (but
//hasn't done so yet)
if(QMetaObject::invokeMethod(worker, "printName",
Qt::BlockingQueuedConnection)) {
qInfo() << "printName called successfully after deletion!";
}
QTimer::singleShot(100, &a, &QCoreApplication::quit);
return a.exec();
}
#include "main.moc"

输出(在Qt 5.9.1、Qt 5.7-窗口、debian上测试(:

my name is  "worker"  in  "worker_thread"
printName called successfully before deletion
blocking  "worker_thread"
destroying  "worker"  in  "worker_thread"
printName called successfully after deletion!

因此,一个简短的答案是:invokeMethod返回true,但没有调用任何内容。但是,请注意,您必须保证工作对象在开始时仍然有效(有关更多详细信息,请参阅最后一点(invokeMethod调用主线程(否则,它是UB(。

以下是我通过挖掘Qt的代码得出的结论列表:

  • ivokeMethod只有在传递给它的参数出现问题时才返回false(例如,插槽签名与参数计数/类型不匹配、返回类型不匹配或连接类型未知等(。请参阅此处
  • 当使用Qt::BlockingQueuedConnection时,invokeMethod通过获取QSemaphore来阻塞调用线程。CCD_ 13被存储到被张贴到接收器对象的CCD_
  • QMetaCallEvent被破坏时,该QSemaphore被释放
  • QObject的析构函数负责调用被析构函数对象的QCoreApplication::removePostedEvents()。这意味着事件队列中以某个对象为目标的所有事件都将在该对象销毁时销毁。请参见此处
  • 您需要确保工作对象在调用线程执行invokeMethod时保持活动状态,直到获得上述信号量,因为invokeMethod可能会在任何时候尝试访问工作对象。我认为这个要求在实践中可能会使事情变得复杂,因为最终可能需要在整个invokeMethod调用中保证对象的生存期(从而避免整个问题(