【发布时间】:2013-07-24 06:59:28
【问题描述】:
我正在尝试使用 C++11 std::condition_variable,但是当我尝试从第二个线程锁定与其关联的 unique_lock 时,我得到一个异常“避免资源死锁”。创建它的线程可以锁定和解锁它,但不是第二个线程,即使我很确定 unique_lock 不应该在第二个线程尝试锁定它时已经被锁定。
FWIW 我在 Linux 中使用 gcc 4.8.1 和 -std=gnu++11。
我已经围绕 condition_variable、unique_lock 和 mutex 编写了一个包装类,所以我的代码中没有其他任何东西可以直接访问它们。注意 std::defer_lock 的使用,我已经掉进了那个陷阱:-)。
class Cond {
private:
std::condition_variable cCond;
std::mutex cMutex;
std::unique_lock<std::mutex> cULock;
public:
Cond() : cULock(cMutex, std::defer_lock)
{}
void wait()
{
std::ostringstream id;
id << std::this_thread::get_id();
H_LOG_D("Cond %p waiting in thread %s", this, id.str().c_str());
cCond.wait(cULock);
H_LOG_D("Cond %p woke up in thread %s", this, id.str().c_str());
}
// Returns false on timeout
bool waitTimeout(unsigned int ms)
{
std::ostringstream id;
id << std::this_thread::get_id();
H_LOG_D("Cond %p waiting (timed) in thread %s", this, id.str().c_str());
bool result = cCond.wait_for(cULock, std::chrono::milliseconds(ms))
== std::cv_status::no_timeout;
H_LOG_D("Cond %p woke up in thread %s", this, id.str().c_str());
return result;
}
void notify()
{
cCond.notify_one();
}
void notifyAll()
{
cCond.notify_all();
}
void lock()
{
std::ostringstream id;
id << std::this_thread::get_id();
H_LOG_D("Locking Cond %p in thread %s", this, id.str().c_str());
cULock.lock();
}
void release()
{
std::ostringstream id;
id << std::this_thread::get_id();
H_LOG_D("Releasing Cond %p in thread %s", this, id.str().c_str());
cULock.unlock();
}
};
我的主线程创建了一个 RenderContext,它有一个与之关联的线程。从主线程的角度来看,它使用 Cond 向渲染线程发出信号以执行操作,并且还可以在 COnd 上等待渲染线程完成该操作。渲染线程在 Cond 上等待主线程发送渲染请求,并在必要时使用相同的 Cond 告诉主线程它已经完成了一个动作。当渲染线程尝试锁定 Cond 以检查/等待渲染请求时,会出现我遇到的错误,此时它根本不应该被锁定(因为主线程正在等待它),更不用说由相同的线程。这是输出:
DEBUG: Created window
DEBUG: OpenGL 3.0 Mesa 9.1.4, GLSL 1.30
DEBUG: setScreen locking from thread 140564696819520
DEBUG: Locking Cond 0x13ec1e0 in thread 140564696819520
DEBUG: Releasing Cond 0x13ec1e0 in thread 140564696819520
DEBUG: Entering GLFW main loop
DEBUG: requestRender locking from thread 140564696819520
DEBUG: Locking Cond 0x13ec1e0 in thread 140564696819520
DEBUG: requestRender waiting
DEBUG: Cond 0x13ec1e0 waiting in thread 140564696819520
DEBUG: Running thread 'RenderThread' with id 140564575180544
DEBUG: render thread::run locking from thread 140564575180544
DEBUG: Locking Cond 0x13ec1e0 in thread 140564575180544
terminate called after throwing an instance of 'std::system_error'
what(): Resource deadlock avoided
说实话,我真的不明白 unique_lock 的用途以及为什么 condition_variable 需要一个而不是直接使用互斥锁,所以这可能是问题的原因。我在网上找不到很好的解释。
【问题讨论】:
-
不要对所有线程使用相同的
unique_lock,这不是它的本意。将它们用作块作用域中的 RAII 对象,而不是类成员。这样,每个调用你的函数的线程都会有自己的实例。另外,请注意虚假唤醒。 -
我明白了,所以每个想要等待或发送通知的上下文都应该使用自己的 unique_lock,但都共享同一个互斥锁?
-
等待,不要发送(
cv.notify()不需要锁)。但除此之外,是的。我将尝试整理一个答案,向您展示如何正确使用这一切,我现在有点忙。 -
我没有意识到 notify() 不需要锁,我想在这种情况下我可以移除一些锁。
-
@syam 感谢您提供示例,但我认为您已经为我很好地回答了这个问题。我已经按照您的建议更改了我的代码以使用 RIIA,它现在可以正常工作。有没有办法可以将您的评论转换为答案,或者我应该根据您的 cmets 做出答案?
标签: linux multithreading c++11 mutex condition-variable