繁体   English   中英

QThread 线程间通信:连接到 &QThread::quit 与连接到 lambda [&thread] { thread->quit();} 的奇怪行为

[英]QThread interthread communication: strange behavior connecting to &QThread::quit vs connecting to a lambda [&thread] { thread->quit();}

我有以下设置:

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // Create the DBManager that will live for the entire
    // duration of the application
    auto dbManagerThread = std::make_unique<QThread>();
    auto dbManager = std::make_unique<DBManager>();
    dbManager->moveToThread(dbManagerThread.get());
    dbManagerThread->start();


    // for the initialization of the application, create the
    // InitHelper object that will utilize the DBManager
    auto initHelper = new InitHelper();
    auto initHelperThread = new QThread();
    initHelper -> moveToThread(initHelperThread);
    // wire InitHelper and its thread
    QObject::connect(initHelperThread, &QThread::started, initHelper, &InitHelper::run);
    QObject::connect(initHelper, &InitHelper::finished, [&initHelperThread] { initHelperThread->quit();});
    QObject::connect(initHelper, &InitHelper::finished, initHelper, &InitHelper::deleteLater);
    QObject::connect(initHelperThread, &QThread::finished, initHelperThread, &QThread::deleteLater);

    // wire InitHelper and DBManager
    QObject::connect(initHelper, &InitHelper::queryDB, dbManager.get(), &DBManager::processQuery);
    QObject::connect(dbManager.get(), &DBManager::queryResult, initHelper, &InitHelper::processQueryResult);


    // block until all information is gathered
    initHelperThread->start();
    initHelperThread->wait();
    std::cout << "Initialization completed." << std::endl;

    // cleanup
    dbManagerThread->quit();
    QObject::connect(dbManagerThread.get(), &QThread::finished, &a, &QCoreApplication::quit);
    return a.exec();
}

这个想法是我有 DBManager,它正在执行异步数据库访问,因此它在整个应用程序中使用。 在应用程序的初始化期间,我需要从数据库中检索一些信息,我想为此使用现有的 DBManager。

但是,由于我需要来自数据库的信息来创建所有其他对象,所以我想阻止主线程的进一步执行,直到我的 InitHelper 从 DBManager 检索到所有必需的信息。

由于上面的代码完全符合它的预期,我认为 InitHelper 和 DBManager 的实现方式并不重要,我在这里省略了它们。

我发现令人困惑的是,我需要在“wire InitHelper 及其线程”部分的第二行中使用 lambda。 如果我更换

QObject::connect(initHelper, &InitHelper::finished, [&initHelperThread] { initHelperThread->quit();});

经过

QObject::connect(initHelper, &InitHelper::finished, initHelperThread, &QThread::quit);

显然线程永远不会关闭,因此“初始化完成”永远不会打印到标准输出。

这是使用与 lambda 的连接时的输出:

InitHelper: Initialization started...
DBManager: processing query...
InitHelper: processing query result
Initialization completed.

而当直接连接到插槽时,我得到以下输出:

InitHelper: Initialization started...
DBManager: processing query...
InitHelper: processing query result

有人知道为什么连接到 lambda 而不是直接连接到插槽时会有区别吗? 我如何在没有 lambda 的情况下编写连接?

在您的代码中:

QObject::connect(initHelper, &InitHelper::finished, [&initHelperThread] { initHelperThread->quit();});

类似于:

QObject::connect(initHelper, &InitHelper::finished, initHelperThread, &QThread::quit, Qt::DirectConnection);

它基本上意味着您直接调用函数而不是使用事件循环。

我相信,您的“正确”代码不起作用的原因(即当您使用 Qt::QueuedConnection - 这是默认设置时)是因为您尚未通过调用a.exec();启动 Qt 事件循环运行a.exec();

为了解决这个问题,我将删除您的 wait() 函数并将您的“启动”代码移动到一个启动类中(有点像您的 initHelper)。 将来自 initHelper 的完成的信号连接到启动类的 start() 函数(或任何您想调用的函数)。

编辑

你也许能够做到这一点(只需阅读类似的内容):

int ret = a.exec();
initHelperThread->wait();
std::cout << "Initialization completed." << std::endl;
    :
  do other stuff
    :
return ret;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM