繁体   English   中英

Qt:立即启动线程,不延迟主事件循环

[英]Qt: Immediately start thread, without delay of main event loop

以下成员函数在主事件循环中运行。

    void MyClass::update()
    {
        Data x = m_interpolator->getRequest(m_interpolationRequest)
        // non blocking new calclulation request
        m_interpolator->asyncRequestCalculations(++m_interpolationRequest);
        // I want to run doCalculations from now on in a second thread
        ... updates ... // takes some time
    }
    <-- earliest time when doCalculations() will be triggered

每次调用update我都要求进行新的计算,该计算将在下一个周期中获取。

CInterpolator (m_interpolator)是另一个线程中的QObject (使用moveToThread )。 asyncRequestCalculations调用(非阻塞) CInterpolator::doCalculations的调用(与向doCalculations插槽发送信号相同)。

它可以工作,但是太慢了。 发生的事是正确安排了doCalculations的信号,但是CInterpolator离开函数update后才调用。 可以理解,这就是Qt事件循环的工作方式。

但是对我来说,这浪费了... updates ...块的时间。 我希望计算与... updates ... 我该怎么做?

主事件循环应该是快速操作,应该连续执行。 因此,这就是为什么您观察到应用程序太慢的原因:刷新速度比应该的慢。

不建议在更新/事件循环中使用慢速操作。

顺便说一句,为了进行并行执行,您必须使用thread 一个代码应该是:

void MyClass::update()
    {
        Data x = m_interpolator->getRequest(m_interpolationRequest)
        // non blocking new calclulation request
        m_interpolator->asyncRequestCalculations(++m_interpolationRequest);
        // I want to run doCalculations from now on in a second thread

       std::thread first(update());
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM