尽管有wakeAll调用,仍有2个线程在QWaitCondition上等待

时间:2013-03-19 10:24:53

标签: c++ multithreading qt vtk

我有线程迭代生成一些几何。我使用VTK进行渲染。在每次迭代后,我想显示(渲染)当前进度。我的方法按预期工作,直到最后2个线程挂起等待QWaitCondition。它们被阻止,即使它们在QWaitCondition的队列中的状态是wokenUp(通过调试器检查)。我怀疑2个线程的数量与我的处理器的4个内核有某种联系。

简化代码如下。我做错了什么以及如何解决它?

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
}

2 个答案:

答案 0 :(得分:2)

您的代码中至少有一个缺陷。 countrender属于关键部分,这意味着需要保护它们免受并发访问。

假设有更多线程等待wc.wait(&renderLock);。有人执行wc.wakeAll();。所有线程都被唤醒了。假设至少有一个线程将notFinished视为真(如果您的任何代码有意义,则 必须 可行)并返回执行:

    threadLock.lock(); //lock scene
    mesh[meshIndex]->setGeometry(g);
    render=true;
    threadLock.unlock();
    wc.wait(&renderLock) <----OOPS...

线程第二次回来时,他没有锁renderLock。所以 Kamil Klimek 是对的:你打电话给等待你没有持有的互斥锁

您应该在构造函数中删除锁,并在调用条件之前锁定。无论您锁定renderlock的哪个位置,该主题都不应该包含threadlock

答案 1 :(得分:0)

问题在于我每个线程需要一个QMutex,而不仅仅是一个全局QMutex。更正后的代码如下。感谢UmNyobe的帮助!

class Logic
{
    QMutex threadLock;
    QWaitCondition wc;
    bool render;
    ...
}

//nothing in constructor related to threading

void Logic::timerProc()
{
    //count was a debugging workaround and is not needed
    if (render)
    {
        threadLock.lock();
        vtkRenderWindow->Render();
        render=false;
        wc.wakeAll();
        threadLock.unlock();
    }
}

double Logic::makeMesh(int meshIndex)
{
    QMutex renderLock; //fix
    renderLock.lock(); //fix
    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
}
相关问题