2013-09-01 32 views
2

我真的試圖將我知道的所有東西結合起來,以使其工作。我相信當前結構中的死鎖或其他線程問題沒有問題。但是,有些部分丟失了,可用的文檔沒有幫助我(我在C++文檔中沒有經驗)。如何映射QVector的QList上的QtConcurrent <double>並將其縮小爲一個QVector

int main() 
{ 
    QVector<double> vector_of_doubles(5); 
    qFill(vector_of_doubles.begin(), vector_of_doubles.end(), 1); 

    QList< QVector<double> > list_of_vectors; 
    list_of_vectors.append(vector_of_doubles); 
    list_of_vectors.append(vector_of_doubles); 
    list_of_vectors.append(vector_of_doubles); 

    QFuture< QVector<double> > res; 
    res = QtConcurrent::mappedReduced(list_of_vectors, Map(), Reduce()); 
    res.waitForFinished(); 
    qDebug()<<res.result(); 
    return 0 
} 

QVector<double> Reduce(QVector<double> vector) 
// Here the vectors get combined into one big vector....but how? 
{ 
    .... 
    return big_vector; 
} 

QVector<double> Map(QVector<double> vector) 
{ 
    for (int i = 0; i < vector.size(); i++) 
    { 
     vector[i] = vector[i] + i; 
    } 
    return vector; 
} 

從保持的QList 3個矢量全部爲一輸入,我想去到一個載體中,爲此,每一個矢量增添一些迭代變量。 我希望爲qDebug()結果看:

{1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4 ,5} 

這裏我認爲是缺失的部分:

  • 有關QtConcurrent:mappedReduced(),我在給什麼正確的參數?
  • 函數返回如何,這些應該如何解決?
  • 究竟需要包含什麼,我只需要包含QtConcurrent?
  • 一旦這個開始工作,並且列表將是巨大的,據我瞭解QtConcurrent將執行線程管理(使用可用的核心),並且列表中的所有項目(即使不是相同的大小)將被傳遞巧妙地對線程不要讓他們閒置?

編輯(也許這是它發生在一個按鈕有問題點擊):

雖然工作爲我的例子,什麼都可以錯了,當我使用這個:

扼流圈:res = QtConcurrent::mappedReduced(holder, correlate, Reduce, QtConcurrent::SequentialReduce);

VAR:QList< QVector<double> > holder;

功能涉及:

QVector<double> correlate(QVector<double> &var1); 

void Reduce(QVector<double> &reduceResult, const QVector<double> &partial)` 

錯誤:沒有匹配函數調用'mappedReduced(QList<QVector<double> >&, <unresolved overloaded function type>, <unresolved overloaded function type>, QtConcurrent::ReduceOption)'

而且還:「無法推斷出模板參數與resultType」

這是同樣的事情作爲工作的事控制檯應用程序。

回答

2

對QtConcurrent的YEP文檔不好。 Here is更好地描述應該如何使用mappedReduced。

所以減少函數應該是這樣的:

void joinVectors(QVector<double> &reduceResult, const QVector<double> &partial) { 
    reduceResult += partial; 
} 

爲了保證數據的正確順序應該添加額外的參數:

res = QtConcurrent::mappedReduced(list_of_vectors, doSomeMapping, joinVectors, QtConcurrent::SequentialReduce); 


這裏是我用於測試的代碼和它的作品(建立成功並給出預期結果):

#include <QCoreApplication> 
#include <QtConcurrent/QtConcurrent> 
#include <QDebug> 

QVector<double> addIndexToVector(QVector<double> vector) { 
    for (int i = 0; i < vector.size(); i++) { 
     vector[i] = vector[i] + i; 
    } 
    return vector; 
} 

void joinVectors(QVector<double> &reduceResult, const QVector<double> &partial) { 
    reduceResult += partial; 
} 

int main(int argc, char *argv[]) { 
    QCoreApplication a(argc, argv); 
    QVector<double> vector_of_doubles(20, 0); 

    QList< QVector<double> > list_of_vectors; 
    list_of_vectors.append(vector_of_doubles); 
    list_of_vectors.append(vector_of_doubles); 
    list_of_vectors.append(vector_of_doubles); 

    QFuture< QVector<double> > res; 
    res = QtConcurrent::mappedReduced(list_of_vectors, addIndexToVector, joinVectors); 
    res.waitForFinished(); 
    qDebug()<<res.result(); 

    return a.exec(); 
} 

項目文件:

QT  += core concurrent 
QT  -= gui 
TARGET = testApp 
CONFIG += console 
CONFIG -= app_bundle 

TEMPLATE = app 

SOURCES += main.cpp 

我用的是Qt 5.0.1 Linux版本。

+0

其中doSomeMapping是我的映射函數,joinVectors是reduce函數,什麼是最後一個參數? – PascalVKooten

+0

啊在文檔中發現了它。我相信我甚至可以使用無序的減少。這個論點是必要的嗎?它似乎經常被忽略。 – PascalVKooten

+0

另外,一切似乎設置正確,但我得到'/usr/include/qt5/QtConcurrent/qtconcurrentmapkernel.h:96:錯誤:未定義的引用'QtConcurrent :: ThreadEngineBase ::〜ThreadEngineBase()'' – PascalVKooten