gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:13:13 25 4
gpt4 key购买 nike

我有以下设置:

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 启动运行的 Qt 事件循环.exec();

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

编辑

您也许可以这样做(只需阅读类似的内容):

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

关于c++ - QThread 线程间通信:连接到 &QThread::quit 与连接到 lambda [&thread] { thread->quit();} 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37499046/

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