boost-threads: How can I pass a scoped_lock to a callee?

708 views Asked by At

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 !

1

There are 1 answers

1
Mike Seymour On BEST ANSWER

Pass it by reference:

void HavingMutex::d(boost::mutex::scoped_lock & lock)
{                                          // ^ that means "reference"
    c->wait(lock);
}