0

我有一个这样的代码,它的功能double myfunction(double)需要很长时间才能完成。

使用 Qt ( https://doc.qt.io/qt-5/qtconcurrentrun.html ),如何使用线程为每个myfunction调用运行循环以减少计算时间?这有意义吗?

std::vector<double> parameters;        //vector full of input values
std::vector<double> results;           //vector to store results in

for(uint i=0; i<parameters.size(); i++)
{
   double parameter = parameters.at(i);
   double result = myfunction(parameter);
   results.push_back(result);
}
4

2 回答 2

2

对于您自己的案例,一个简单的示例用法QtConcurrent::run类似于(未经测试)...

std::vector<double> parameters;     // vector full of input values.
std::vector<double> results;        // Empty vector to store results.

/*
 * Each call to QtConcurrent::run returns a QFuture.  Store
 * these so we can wait on the results.
 */
std::vector<QFuture<double>> futures;

/*
 * Start all tasks and store the QFutures.
 */
for (auto parameter: parameters) {
    futures.push_back(QtConcurrent::run(&myfunction, parameter));
}

/*
 * Wait for each QFuture in turn and save its result.
 */
for (auto &f: futures) {
    results.push_back(f.result());
}
于 2020-05-13T11:48:55.957 回答
0

使用QtConcurrent::mapped()and aQFutureWatcher使所有处理异步发生。

QFutureWatcherfinished()当所有处理完成时发出信号。结果由一个QFutureQFutureWatcher. 最重要的是,用户界面永远不会冻结。

my_project.pro

# must include "concurrent" to use QtConcurrent::mapped()
QT += concurrent
...

映射处理器.h

#include <QObject>
#include <QFutureWatcher>
#include <vector>

class MappedProcessor : public QObject
{
    Q_OBJECT
public:
    explicit MappedProcessor(QObject *parent = nullptr);

public slots:
    void processValues();

private slots:
    void handleProcessingFinished();

private:
    static double myFunction(double value);

    QFutureWatcher<double> m_futureWatcher;
};

映射处理器.cpp

#include "MappedProcessor.h"
#include <QtConcurrent/QtConcurrent>
#include <QList>
#include <QThread>

MappedProcessor::MappedProcessor(QObject *parent) : QObject(parent)
{
    connect(&m_futureWatcher, &QFutureWatcher<double>::finished,
            this, &MappedProcessor::handleProcessingFinished);
}

void MappedProcessor::processValues()
{
    // create some values to process
    std::vector<double> parameters;
    for (auto i = 0; i < 16; ++i)
        parameters.push_back(i);

    auto sequence = QVector<double>::fromStdVector(parameters);
    auto future = QtConcurrent::mapped(sequence, myFunction);
    m_futureWatcher.setFuture(future);
}

void MappedProcessor::handleProcessingFinished()
{
    // convert the results to std::vector<double>
    auto theResults = m_futureWatcher.future().results().toVector().toStdVector();

    qDebug() << "Received" << theResults.size() << "results";
}

// pretend to be a long running calculation...
double MappedProcessor::myFunction(double value)
{
    QThread::msleep(10000);
    return value;
}
于 2021-04-18T02:50:59.400 回答