QtConcurrent::map()、QtConcurrent::mapped()和QtConcurrent::mappedReduced()函数可以对一个序列中(如QList、QVector等)的项目并行地进行计算。
qtconcurrent-map
1 2 3
|
$ qmake $ make $ ./qtconcurrent-map-demo
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void my_fun1(int &value) { qDebug() << "run my_fun1 thread id:" << QThread::currentThreadId(); value += 1; }
void MainWindow::onMapBtnClicked() { QList<int> m_list; m_list << 1 << 3 << 5 << 7 << 9;
QFuture<void> future = QtConcurrent::map(m_list, my_fun1); future.waitForFinished(); qDebug() << "m_list: "<< m_list; }
|