Skip to content

Instantly share code, notes, and snippets.

@ArtyomLazyan
Created October 25, 2018 19:39
Show Gist options
  • Save ArtyomLazyan/5cce458407b8d6b8f5c0c008ef83826a to your computer and use it in GitHub Desktop.
Save ArtyomLazyan/5cce458407b8d6b8f5c0c008ef83826a to your computer and use it in GitHub Desktop.
cpp
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