我已經線程迭代生成一些幾何。我使用VTK進行渲染。每次迭代後,我想顯示(呈現)當前的進度。我的方法按預期工作,直到最後2個線程仍在等待QWaitCondition。即使它們在QWaitCondition隊列中的狀態爲wokenUp(通過調試器檢查),它們也會被阻止。我懷疑有2個線程的數量以某種方式與我的處理器的4個內核連接。2個線程仍然掛在QWaitCondition上,儘管全部調用
簡化代碼如下。我做錯了什麼,以及如何解決它?
class Logic
{
QMutex threadLock, renderLock;
//SOLUTION: renderLock should be per thread, not global like this!
QWaitCondition wc;
bool render;
...
}
Logic::Logic()
{
...
renderLock.lock(); //needs to be locked for QWaitCondition
}
void Logic::timerProc()
{
static int count=0;
if (render||count>10) //render wanted or not rendered in a while
{
threadLock.lock();
vtkRenderWindow->Render();
render=false;
count=0;
wc.wakeAll();
threadLock.unlock();
}
else
count++;
}
double Logic::makeMesh(int meshIndex)
{
while (notFinished)
{
...(calculate g)
threadLock.lock(); //lock scene
mesh[meshIndex]->setGeometry(g);
render=true;
threadLock.unlock();
wc.wait(&renderLock); //wait until rendered
}
return g.size;
}
void Logic::makeAllMeshes()
{
vector<QFuture<double>> r;
for (int i=0; i<meshes.size(); i++)
{
QFuture<double> future = QtConcurrent::run<double>(this, &Logic::makeMesh, i);
r.push_back(future);
}
while(any r is not finished)
QApplication::processEvents(); //give timer a chance
}
您必須呼叫等待鎖定互斥! – 2013-03-19 11:10:26
我這樣做:wc.wait(&renderLock);還是需要一些額外的電話? – 2013-03-19 11:38:16