我是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()中,我需要访问我在()中获取的作用域锁,以便我可以等待它。 (其他一些线程会通知)。
任何有用的帮助。谢谢!
答案 0 :(得分:0)
你试过简单的参考吗?根据{{3}}处的boost 1.41文档,它应该是所有必需的。 ...
void HavingMutex::a()
{
boost::mutex::scoped_lock lock(m);
...
b(lock);
}
void HavingMutex::b(boost::mutex::scoped_lock &lock)
{
if (some condition) // consider while (!some_condition) here
d(lock);
}
void HavingMutex::d(boost::mutex::scoped_lock &lock)
{
c->wait(lock);
}
void HavingMutex::notify()
{
// set_condition;
c->notify_one();
}
同样在增强示例中,他们在循环等待期间。系统本身在某些情况下可能会中断等待,而不是真正的锁定。
我建议你重新考虑让静态成员使用静态方法。而是将它们创建为普通成员,并创建一个全局对象。