2014-10-30 67 views
-1

我必須反覆運行同一個函數來處理大量數據。QtConcurrent ::在創建新線程後繼續運行

這是我將使用QtConcurrent::run的地方,以便我可以同時處理5個對象。這是我的代碼,它是通過對每個數據集的意思toloop:

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個dataSet(並獲得了成功的結果)?

回答

2
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(); 
}