C++ QThreads从未退出,尽管已到达发射完成

C++ QThreads从未退出,尽管已到达发射完成,c++,multithreading,qt,qt5,C++,Multithreading,Qt,Qt5,以下场景: void Tool::on_btn_StartMeasure() { TestExecution *testExecution = new TestExecution(); QThread *workerThread = new QThread(); connect(workerThread, &QThread::started, testExecution, &TestExecution::DoTest); connect(test

以下场景:

void Tool::on_btn_StartMeasure()
{
    TestExecution *testExecution = new TestExecution();
    QThread *workerThread = new QThread();

    connect(workerThread, &QThread::started, testExecution, &TestExecution::DoTest);
    connect(testExecution, &TestExecution::Finished, workerThread, &QThread::quit);

    testExecution->moveToThread(workerThread);
    workerThread->start();
}
如果用户按下“StartMeasure”按钮,我将启动一个新的工作线程,以避免GUI中的阻塞行为

在那里,我再次启动n个用于硬件通信的新线程(出于速度原因,需要并行流)->发送/接收某些内容并评估响应

线程模型:

void Tool::on_btn_StartMeasure()
{
    TestExecution *testExecution = new TestExecution();
    QThread *workerThread = new QThread();

    connect(workerThread, &QThread::started, testExecution, &TestExecution::DoTest);
    connect(testExecution, &TestExecution::Finished, workerThread, &QThread::quit);

    testExecution->moveToThread(workerThread);
    workerThread->start();
}

问题是n个工作线程(用于硬件通信)从未退出

简化代码:

void Tool::on_btn_StartMeasure()
{
    TestExecution *testExecution = new TestExecution();
    QThread *workerThread = new QThread();

    connect(workerThread, &QThread::started, testExecution, &TestExecution::DoTest);
    connect(testExecution, &TestExecution::Finished, workerThread, &QThread::quit);

    testExecution->moveToThread(workerThread);
    workerThread->start();
}
测试执行类

class TestExecution : public QObject
{
    Q_OBJECT

    public:
        void DoTest();

    signals:
         void Finished();

};

void TestExecution::DoTest()
{
    std::vector<std::shared_ptr<TestFlow>> objects;
    std::vector<QThreads*> workerThreads;        

    for(int n=0; n<numberOfDevices; n++)
    {
        workerThreads.push_back(new QThread());
        objects.push_back(std::make_shared<TestFlow>());
        connect(workerThreads.back(), &QThread::started, objects.back().get(), &TestFlow::DoWork);
        connect(objects.back().get(), &TestFlow::Finished, workerThreads.back(), &QThread::quit);

        objects.back()->moveToThread(workerThreads.back());
        workerThreads.back()->start;
    }    

    // wait for worker threads to finish
    for(auto it=workerThreads.begin(); it!=workerThreads.end(); it++)
    {
        (*it)->wait(30); // wait a maximum of 30s -> for testing
    }

    // do something with the result <--- NEVER REACHED

    emit Finished();
}
class TestFlow : public QObject
{
    Q_OBJECT

    public:
        void DoWork();

    signals:
         void Finished();

};

void TestFlow::DoWork()
{
    // do something
    emit Finished(); <-- REACHED
}

看起来还好吧

在将对象移动到其他线程之前,先连接信号和插槽。
Qt::AutoConnect
用于
connect()
中,此时两个对象位于同一线程中。我不确定当您将目标对象移动到其他线程时,它是否会自动更改连接的属性。我猜,它想调用您创建并连接对象的线程中的插槽,但该线程正在
wait()
。顺便说一句,检查线程很有用,例如,放置
qDebug()@EvgenyS。qDebug的输出:工具::on_btn_StartMeasure,currentThread QThread(0x601b08),object thread QThread(0x601b08)| TestExecution::DoTest(),currentThread QThread(0x388bf68),object thread QThread(0x388bf68)| TestFlow::DoWork(),currentThread QThread(0x3958690),object thread QThread(0x3958690)我看起来还可以吗@叶甫盖尼斯。我还改变了连接信号的顺序!同样的问题!好的,看起来很奇怪。为什么不直接在
DoWork()
中调用
QMetaObject::invokeMethod(thread(),“quit”,Qt::QueuedConnection)
,而不是
emit Finished()?会有帮助吗?