문제

I'm new to the boost threads library. I have a situation where I acquire a scoped_lock in one function and need to wait on it in a callee.

The code is on the lines of:

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)
}

Basically, in function d(), I need to access the scoped lock I acquired in a() so that I can wait on it. How do I do that ? (Some other thread will notify).

Or can I directly wait on a mutex instead of a lock ?

Any help is appreciated. Thanks !

도움이 되었습니까?

해결책

Pass it by reference:

void HavingMutex::d(boost::mutex::scoped_lock & lock)
{                                          // ^ that means "reference"
    c->wait(lock);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top