- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有以下设置:
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/
可以使用 lambda 和函数创建有序对(Lisp 中的缺点),如 Use of lambda for cons/car/cdr definition in SICP 所示。 它也适用于 Python
我正在尝试从另一个调用一个 AWS lambda 并执行 lambda 链接。这样做的理由是 AWS 不提供来自同一个 S3 存储桶的多个触发器。 我创建了一个带有 s3 触发器的 lambda。第一
根据以下源代码,常规 lambda 似乎可以与扩展 lambda 互换。 fun main(args: Array) { val numbers = listOf(1, 2, 3) f
A Tutorial Introduction to the Lambda Calculus 本文介绍乘法函数 The multiplication of two numbers x and y ca
我想弄清楚如何为下面的表达式绘制语法树。首先,这究竟是如何表现的?看样子是以1和2为参数,如果n是 0,它只会返回 m . 另外,有人可以指出解析树的开始,还是一个例子?我一直找不到一个。 最佳答案
在 C++0x 中,我想知道 lambda 函数的类型是什么。具体来说: #include type1 foo(int x){ return [x](int y)->int{return x * y
我在其中一个职位发布中看到了这个问题,它询问什么是 lambda 函数以及它与高阶函数的关系。我已经知道如何使用 lambda 函数,但不太自信地解释它,所以我做了一点谷歌搜索,发现了这个:What
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
Evaluate (((lambda(x y) (lambda (x) (* x y))) 5 6) 10) in Scheme. 我不知道实际上该怎么做! ((lambda (x y) (+ x x
我正在处理 MyCustomType 的实例集合如下: fun runAll(vararg commands: MyCustomType){ commands.forEach { it.myM
Brian 在他对问题 "Are side effects a good thing?" 的论证中的前提很有趣: computers are von-Neumann machines that are
在 Common Lisp 中,如果我希望两个函数共享状态,我将按如下方式执行 let over lambda: (let ((state 1)) (defun inc-state () (in
Evaluate (((lambda(x y) (lambda (x) (* x y))) 5 6) 10) in Scheme. 我不知道实际上该怎么做! ((lambda (x y) (+ x x
作为lambda calculus wiki说: There are several possible ways to define the natural numbers in lambda cal
我有一个数据类,我需要初始化一些 List .我需要获取 JsonArray 的值(我使用的是 Gson)。 我做了这个函数: private fun arrayToList(data: JsonAr
((lambda () )) 的方案中是否有简写 例如,代替 ((lambda () (define x 1) (display x))) 我希望能够做类似的事情 (empty-lam
我在 Java library 中有以下方法: public void setColumnComparator(final int columnIndex, final Comparator colu
我正在研究一个函数来计算国际象棋游戏中棋子的有效移动。 white-pawn-move 函数有效。当我试图将其概括为任一玩家的棋子 (pawn-move) 时,我遇到了非法函数调用。我已经在 repl
考虑这段代码(在 GCC 和 MSVC 上编译): int main() { auto foo = [](auto p){ typedef decltype(p) p_t;
我正在阅读一个在 lambda 内部使用 lambda 的片段,然后我想通过创建一个虚拟函数来测试它,该函数从文件中读取然后返回最大和最小数字。 这是我想出来的 dummy = lambda path
我是一名优秀的程序员,十分优秀!