Created
September 28, 2011 22:59
-
-
Save embix/1249497 to your computer and use it in GitHub Desktop.
QtConcurrent::run spike solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QList> | |
#include <QtAlgorithms> | |
#include <QDebug> | |
#include <QtConcurrentRun> | |
QList<int> sort(QList<int> unsorted, int chunkId){ | |
qDebug() << "starting to sort chunk " << chunkId; | |
qSort(unsorted); | |
qDebug() << "finished to sort chunk " << chunkId; | |
return unsorted; | |
} | |
void printList(const QList<int> &list){ | |
foreach(const int i, list){ | |
qDebug() << i; | |
} | |
} | |
int main(int argc, char *argv[]) { | |
QList<QList<int> > chunks; | |
int chunkCount = 1<<4; | |
int chunkSize = 1<<22; | |
for(int i = 0; i != chunkCount; ++i){ | |
qDebug() << "creating Chunk " << i; | |
QList<int> actChunk; | |
for(int j = 0; j != chunkSize; ++j){ | |
actChunk.append(qrand()); | |
} | |
chunks.append(actChunk); | |
} | |
qDebug() << "chunks created"; | |
qDebug() << "sorting"; | |
QList<QFuture<QList<int> > > sortedChunkFutures; | |
for(int i = 0; i != chunkCount; ++i){ | |
QList<int> actChunk = chunks.at(i); | |
QFuture<QList<int> > actChunkSortedFuture; | |
actChunkSortedFuture = QtConcurrent::run(sort, actChunk, i); | |
sortedChunkFutures.append(actChunkSortedFuture); | |
} | |
qDebug() << "joining in same order"; | |
for(int i = 0; i != chunkCount; ++i){ | |
QFuture<QList<int> > actChunkSortedFuture; | |
actChunkSortedFuture = sortedChunkFutures.at(i); | |
qDebug() << "joining " << i; | |
QList<int> sortedChunk = actChunkSortedFuture.result(); | |
qDebug() << "joined " << i; | |
} | |
qDebug() << "finished"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment