首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我怎么能看多个QFuture?

我怎么能看多个QFuture?
EN

Stack Overflow用户
提问于 2020-10-31 10:04:10
回答 2查看 410关注 0票数 1

当它正在观看的QFutureWatcher类完成时,finished()类会发出信号finished()。我怎么能看多个QFuture?我正在使用QtConcurrent::run()并行运行两个线程,希望在两个线程完成后获得一个信号。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-31 11:33:09

我会这样处理这个问题:

  1. 根据需要创建尽可能多的QFutureWatcher
  2. 将所有QFutureWatchers添加到列表中,resp。向量,例如m_futureWatchers: m_futureWatchers.append(futureWatcher);
  3. QFutureWatcher::finished信号连接到同一个时隙,例如handleFinished: 连接(futureWatcher,&QFutureWatcher::connect,this,MyClass::handleFinished);
  4. handleFinished槽中,检查QFutureWatcher::isFinished并作出相应反应: bool allAreFinished = true;for (auto*futureWatcher: m_futureWatchers) allAreFinished &= futureWatcher->isFinished();if (allAreFinished) { // doSomething }

注意:对于两个未来的观察者来说,可能更容易一些,比如有两个成员变量,例如m_futureWatcher1m_futureWatcher1,而不是一个列表,然后像这样在handleFinished槽中检查它们:

代码语言:javascript
复制
if (m_futureWatcher1->isFinished() && m_futureWatcher2->isFinished) {
    ...
}
票数 2
EN

Stack Overflow用户

发布于 2020-10-31 11:44:15

您可以为Qt使用第三方AsyncFuture库:

将具有不同类型的多个期货组合成一个未来的单一对象:

代码语言:javascript
复制
/* Combine multiple futures with different type into a single future */

QFuture<QImage> f1 = QtConcurrent::run(readImage, QString("image.jpg"));

QFuture<void> f2 = observe(timer, &QTimer::timeout).future();

QFuture<QImage> result = (combine() << f1 << f2).subscribe([=](){
    // Read an image but do not return before timeout
    return f1.result();
}).future();

QCOMPARE(result.progressMaximum(), 2);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64620530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档