【问题标题】:QtConcurrent::run proceed after new thread has been createdQtConcurrent::run 在创建新线程后继续
【发布时间】:2014-10-30 18:16:27
【问题描述】:

对于大量数据,我必须一遍又一遍地运行相同的函数。

这是我将使用QtConcurrent::run 的地方,这样我就可以同时处理 5 个对象。这是我的代码,用于循环遍历每个数据集:

    for (int j = 0; j < (dataset.size())/3; j++){
        int i = 0;
        while (i < 2) {
            QtConcurrent::run(this, &SomeObject::doWork, dataset.at(i+3*j));
            i++;
        }
    }

问题:我注意到有时doWork 不会针对数据集中的某些数据调用。正如this QT Documentation 所说,我相信没有为丢失的数据创建线程。在调用下一对之前,如何确保我同时处理 2 个数据集(并获得成功的结果)?

【问题讨论】:

    标签: c++ qt concurrency qt5


    【解决方案1】:
    for (int j = 0; j < (dataset.size())/3; j++){
        QFutureSynchronizer<void> synchronizer;
        int i = 0;
        while (i < 2) {
            synchronizer.addFuture(QtConcurrent::run(this, &SomeObject::doWork, dataset.at(i+3*j)));
            i++;
        }
        synchronizer.waitForFinished();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 2018-01-12
      相关资源
      最近更新 更多