[英]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.