Skip to content

Instantly share code, notes, and snippets.

@embix
Created September 28, 2011 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save embix/1249497 to your computer and use it in GitHub Desktop.
Save embix/1249497 to your computer and use it in GitHub Desktop.
QtConcurrent::run spike solution
#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