当App即将退出时,QtConcurrent等待完成

时间:2016-05-18 11:43:30

标签: c++ multithreading qt qtconcurrent

我找不到任何明确的解决方案。我有一个以QtConcurrent::run()开头的帖子。在线程完成之前关闭应用程序时,应用程序崩溃。我希望应用程序在所有后台线程(QtConcurrent::run())完成后关闭。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我来到这里寻找和你一样的东西,最终以我自己的方式解决它。这是我的方法:

// [...] All necessary includes would go here

int main(int argc, char *argv[])
{
    // Keep track of time
    quint64 start=QDateTime::currentMSecsSinceEpoch();

    // Qt app instance
    QCoreApplication app(argc, argv);

    // Someplace safe to keep our futures
    QList<QFuture<void> > futures;

    //Prepare the lambda that does the heavy lifting
    auto lambda = [&] (void) {
        // [...] Heavy lifting goes here
    };

    // Run up some processing
    for(/* any number of heavy liftings we need */){
        // Keep the returned future
        auto future =  QtConcurrent::run(lambda, /* parameters would go here*/ );
        // Store the future around for later
        futures.append(future);
    };

    //Now all the heavy lifting has started, and we are ready to wait for them all to complete.
    for(auto future:futures){
        // Note that if the future finished BEFORE we call this, it will still work.
        future.waitForFinished();
    }

    // Spit out the number of seconds we were running
    quint64 end=QDateTime::currentMSecsSinceEpoch();
    qDebug()<<"DONE after" <<(((qreal)end-start)/1000.0)<<" sec";

    //NOTICE: I did not need an event loop so no app.exec() call here
}

更新2018年

自从我写完这个答案变得更加明智,并决定分享另一种方式来做到这一点在某些情况下会更优雅,并节省一些打字/样板。它被称为map-reduce,好的一点是你不需要所有这些,只需map部分就能正常工作。

注意:这是官方文档中this example的快速修改。如果您想保留一些输出数据,请参阅此示例。

// [...] All necessary includes would go here

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);


    // Create a list containing data to be processed
    QList<MyClass> tasks;

    // At this point fill the list of tasks with whatever you need

    // This is the actual code that will run per task
    std::function<void(const MyClass&)> myOperation = [](const MyClass &task)
    {
        qDebug() << "Doing myOperation() in thread" << QThread::currentThread();
        // Do heavy lifting on task object here
    };

    // Start the processing. QConcurrent will automagically start up threads, distribute the tasks and run them taking care of all the tedious threads management for you.
    // It can also take care of collecting the output (not shown in this example).
    // Finally , and most importantly the answer to the question; using the blockingMap will actually block execution until all the work is done.
    QtConcurrent::blockingMap(tasks, myOperation);

    return 0;
}