0

我在网上找到了几个教程,解释了如何在长时间计算期间更新 QProgressBar。其中之一是:使用 QThread 进行计算,然后发出连接到progressBar.setValue(int).

我认为这也必须适用于同时运行的几个 QThread,但有些东西不能正常工作。

所以,这就是我要做的:我想计算几个粒子的轨迹(每个粒子都有一个长循环)。为了使用多核处理,我为这些粒子中的每一个创建了一个 QThread,并让它调用各自的计算方法。这工作正常,所有核心都被使用,计算完成时间比以前大约四分之一。

我根据本教程http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/编写了一个 Worker 类。标头如下所示:(worker.h)

#include <world.h>

class Worker: public QObject
{
    Q_OBJECT

public:
    explicit Worker(World *world = 0, double deltaTau = 0., double maxDist = 0., double iterations = 0., double index = 0);

public slots:
    void process();

signals:
    void finished();
    void eror(QString err);

private:
    World *w;
    double my_deltaTau;
    double my_maxDist;
    int my_iterations;
    int my_index;
};

像这样的来源:(worker.cpp)

#include "worker.h"

Worker::Worker(World *world, double deltaTau, double maxDist, double iterations, double index)
{
    w = world;
    my_deltaTau = deltaTau;
    my_maxDist = maxDist;
    my_iterations = iterations;
    my_index = index;
}

void Worker::process()
{
    w->runParticle(my_deltaTau, my_maxDist, my_iterations, my_index);
    emit finished();
}

在 world.cpp 中,我有一个run启动所有线程的函数和runParticleWorker 调用的函数:

void World::run(double deltaTau, double maxDist, int iterations)
{
    globalProgress = 0;
    for (int j = 0; j < particles->size(); j++) { //loop over all particles
        QThread *thread = new QThread;
        Worker *worker = new Worker(this, deltaTau, maxDist, iterations, j);
        worker->moveToThread(thread);
        connect(thread, SIGNAL(started()), worker, SLOT(process()));
        connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
        connect(worker, SIGNAL(finished()), thread, SLOT(deleteLater()));
        connect(thread, SIGNAL(finished()), worker, SLOT(deleteLater()));
        thread->start();
    }
}

void World::runParticle(double deltaTau, double maxDist, int iterations, int index)
{
    for (int i = 0; i < iterations; i++) { //loop over iteration steps
        if (i % 1000 == 0) { //only update the progress bar every 1000th iteration
            emit updateProgress(++globalProgress);
            qApp->processEvents(); // <--- I added this line, no effect!
        }
        [...] // <--- do my calculations for the particle's trajectories
    }
}

updateProgress(int)每 1000 次迭代步骤调用一次公共槽。它连接到我的 MainWindow 中的 QProgressBar,如下所示:

progressBar->setValue(0);
progressBar->setMaximum(nrPart * iter / 1000); //number of particles * number of iteration steps / 1000
connect(world, SIGNAL(updateProgress(int)), progressBar, SLOT(setValue(int)));
world->run(timeStep, dist, iter);

我的问题是进度条在所有计算完成之前不会移动,然后我看到它很快就会移动到 100%。

有没有人看到我的错误或知道如何正确地做到这一点?

编辑

我做了以下更改:

(工人.h)

#include "world.h"

class Worker: public QObject
{
    Q_OBJECT

public:
    explicit Worker(World *world = 0, Particle *particle = 0, QList<MagneticField> *bfields = 0, double deltaTau = 0., double maxDist = 0., int iterations = 0);

public slots:
    void process();

signals:
    void finished();
    void updateProgress(int value);
    void ProcessParticle();
    void eror(QString err);

private:
    int i;
    Particle *p;
    QList<MagneticField> *magneticFields;
    double my_deltaTau;
    double my_maxDist;
    int my_iterations;
};

(工人.cpp)

#include "worker.h"

Worker::Worker(World *world, Particle *particle, QList<MagneticField> *bfields, double deltaTau, double maxDist, int iterations)
{
    i = 0;
    const World *w = world;
    p = particle;
    magneticFields = bfields;
    my_deltaTau = deltaTau;
    my_maxDist = maxDist;
    my_iterations = iterations;
    connect(this, SIGNAL(updateProgress(int)), w, SLOT(updateTheProgress(int)));
    connect(this, SIGNAL(ProcessParticle()), this, SLOT(process()), Qt::QueuedConnection);
}

void Worker::process()
{
    const int modNr = my_iterations / 1000;
    QDateTime start = QDateTime::currentDateTime();
    while (i < my_iterations) { //loop over iteration steps
        [...] // <--- do my calculations
        //handle progress
        emit updateProgress(1);
        if (QDateTime::currentDateTime() > start.addMSecs(300)) {
            emit ProcessParticle();
            ++i; //ensure we return to the next iteration
            return;
        }
        i++;
    }
    qDebug() << "FINISHED"; // <--- I can see this, so finished() should be emitted now...
    emit finished();
}

(world.h 的一部分)

public slots:
    void threadFinished();
    void updateTheProgress(int value);

signals:
    void updateProgress(int value);

(world.cpp 的一部分)

void World::threadFinished()
{
    particleCounter++;
    qDebug() << "particles finished: " << particleCounter; // <--- this is NEVER called !?!?
    if (particleCounter == particles->size()) {
        hasRun = true;
    }
}

void World::updateTheProgress(int value)
{
    globalProgress += value;
    emit updateProgress(globalProgress);
}

void World::run(double deltaTau, double maxDist, int iterations)
{
    globalProgress = 0;
    particleCounter = 0;
    hasRun = false;
    for (int i = 0; i < particles->size(); i++) { //loop over all particles
        QThread *thread = new QThread;
        Worker *worker = new Worker(this, &(*particles)[i], bfields, deltaTau, maxDist, iterations);
        worker->moveToThread(thread);
        connect(thread, SIGNAL(started()), worker, SLOT(process()));
        connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
        connect(worker, SIGNAL(finished()), thread, SLOT(deleteLater()));
        connect(worker, SIGNAL(finished()), this, SLOT(threadFinished())); // <--- this connection SHOULD make sure, I count the finished threads
        connect(thread, SIGNAL(finished()), worker, SLOT(deleteLater()));
        thread->start();
    }
}

(在 MainWindow.cpp 的某处)

progressBar->setValue(0);
progressBar->setMaximum(nrPart * iter);
connect(world, SIGNAL(updateProgress(int)), progressBar, SLOT(setValue(int)));
world->run(timeStep, dist, iter);
while (!world->hasBeenRunning()) {} //wait for all threads to finish

正如我在上面的代码中标记的那样,当线程完成时我永远不会收到通知,并且我最终在 MainWindow 中陷入无限循环。World <-> Worker 连接还有问题吗?

4

2 回答 2

1

问题是为了处理发出的信号,您需要允许事件循环在新线程上运行。通过保留在 runParticle 的 for 循环中,直到函数完成,这才发生。

有一种粗略的方法可以解决这个问题,即在循环期间每隔一段时间调用一次 QApplication::processEvents。

更好的方法是重新设计对象,以便在退出并允许事件循环自然运行之前处理多次迭代。

因此,要为处理创建一个时间片,在您的 for 循环中,您需要计算迭代花费的时间。如果时间已超过 1/30 秒,则调用 QueuedConnection 类型的信号再次调用您的插槽函数并退出 for 循环。

QueuedConnection 将确保处理任何事件,然后再次调用您的函数。

假设 runParticle 是一个插槽:-

void Worker::runParticle(...)
{
    static int i = 0;

    QDateTime start = QDateTime::currentDateTime();

    for(i; i < iteration; ++i)
    {
        // do processing


        emit updateProgress(++globalProgress);

        // check if we've been here too long
        if(QDateTime::currentDateTime() > start.addMSecs(300))
        {
          emit ProcessParticle(); // assuming this is connected to runParticle with a Queued Connection
          ++i; // ensure we return to the next iteration
          return;
        }
    }
}

另一方面,当一个对象被移动到一个新线程时,它的所有子对象也会被移动,其中一个子对象是 QObject 层次结构的一部分。

通过让 Worker 对象持有指向 World 的指针,它直接调用 World 的 runParticle 函数,该函数仍在主线程上。虽然这是不安全的,但这也意味着 runParticle 函数正在主线程上处理。

您需要将 runParticle 函数移动到新线程上的 Worker 对象。

于 2014-06-11T15:56:03.520 回答
1

海事组织这是错误的。创建线程的成本很高,并且您想要创建很多线程。首先,您应该使用QThreadPool,您的案例与此类的功能完全匹配。

还有一些QtConcurrent方法可以大大减少样板代码(这是 Qt 的废弃功能,因此建议使用 QThreadPool,但您可以尝试它的效果很好)。

于 2014-06-12T15:59:20.893 回答