Created
October 25, 2018 19:39
-
-
Save ArtyomLazyan/5cce458407b8d6b8f5c0c008ef83826a to your computer and use it in GitHub Desktop.
cpp
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
QList<QVector<int> > resultContainter; | |
QVector<int> Calculate(int begin, int end) | |
{ | |
QVector<int> localResult(30); | |
for (int i = begin; i < end; ++i) | |
{ | |
localResult[aData[i]]++; | |
} | |
return localResult; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
int nThreadCount = QThread::idealThreadCount(); | |
int nOffset = qCeil(aData.count() / (double)nThreadCount); | |
QFutureSynchronizer<QVector<int>> synchronizer; | |
for (int i = 0; i < aData.count(); i += nOffset) | |
{ | |
if ((i + nOffset) >= aData.count()) | |
break; | |
synchronizer.addFuture(QtConcurrent::run(Calculate, i, i + nOffset)); | |
} | |
synchronizer.waitForFinished(); | |
for (int i = 0; i < synchronizer.futures().count(); i++) | |
{ | |
resultContainter.append(synchronizer.futures().at(i).result()); | |
} | |
synchronizer.clearFutures(); | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment