【发布时间】:2012-08-01 15:32:00
【问题描述】:
我是 boost 线程库的新手。我有一种情况,我在一个函数中获得了scoped_lock,需要在被调用者中等待它。
代码在以下几行:
class HavingMutex
{
public:
...
private:
static boost::mutex m;
static boost::condition_variable *c;
static void a();
static void b();
static void d();
}
void HavingMutex::a()
{
boost::mutex::scoped_lock lock(m);
...
b() //Need to pass lock here. Dunno how !
}
void HavingMutex::b(lock)
{
if (some condition)
d(lock) // Need to pass lock here. How ?
}
void HavingMutex::d(//Need to get lock here)
{
c->wait(lock); //Need to pass lock here (doesn't allow direct passing of mutex m)
}
基本上,在函数d() 中,我需要访问我在a() 中获得的作用域锁,以便我可以等待它。我怎么做 ? (其他一些线程会通知)。
或者我可以直接等待互斥体而不是锁吗?
感谢任何帮助。谢谢!
【问题讨论】:
标签: c++ boost boost-thread boost-mutex scoped-lock