Copying boost::mutex and boost::condition_variable between two different classes.

Hey Guys,

I have a problem I have not been able to find a solution to. My class looks like this :

class B;
// Class Declaration

Class A //name changed
boost::mutex mut_a;
boost::condition_variable cond_a;
B b;



// constructor
A::A() :
b(mut_a, cond_a)

// A function
boost::unique_lock<boost::mutex> locker(mut_a)
//so dome things
cond_a.notify_all(); OR b.NotifyCond();



Class B
boost::mutex& mut;
boost::condition_variable& cond;
bool boolvar;

B(boost::mutex&, boost::condition_variable&);
inline SetBBoolVar() { boolvar = true;}
inline ClearBBoolVar() { boolvar = false;}
NotifyCond() { cond.notify_all();}

B::B(boost::mutex& imut, boost::condition_variable& icond):
mut(imut), cond(icond)

boost::unique_lock<boost::mutex> locker2(mut);
while (1) {
while(!boolVar) {
cond.wait(); }
//do something important

Now the problem with this code is that it doesn't notify the wait. I never get past the wait in class B. I have also tried creating a function to change the condition variable in class B only and not passing the condition_variable by reference. But still it doesn't work.

Can someone point out if boost::mutex, when passed like this would create problems ?

Also, I have changed the code as I can't post the exact code, so disregard any silly mistakes, they don't exist in the real code.

Thank you all for your time.

ONE MORE THING : boost::condition_variable referencing forced my to write a copy reference constructor to class B which I have done.
Last edited on
Nothing jumps out at me as being wrong.

Though there might be nothing wrong in what you posted... that's the problem with transcribed psuedo-code.

If you can't post the full program, then make a smaller version that we can actually compile and run which reproduces the problem. That will make it much easier for us (and you) to debug it.
Hey Disch,

I think I figured out the problem. Can you please look into the next question as I couldn't find a good reference to this question.

1. When I create a new thread out of a class, using boost:: thread, is that a copy construction being used ? If So , here lies the problem when I set my boolean, there is acutally different set of boolean from different object being initialized and not the one I want.

2. This also creates a problem if I don't pass boost::condition_variable by reference cause now it notifies some other condition variable and hence never does What i Want.

So , the question is how do I make a reference to the same class object and not a copy of it. I assume this is true because, boost:: thread documentation says the class which is being turned into thread has to be copyable.

Another way could be initializing and object of a class only using a boost thread, if that is in anyway an elegant thing to do.
Last edited on
You can pass arguments to boost/std::thread's constructor by reference if you wrap then in boost/std::ref
Hey Disch,

I think Boost::ref is my ans. Let me know if I am wrong anywhere !

I got the result I expected in a small trial code. I am jumping into the main code now.

Thanks for the help !


@Cubbi :

Hey Cubbi. Yeah Thats what I did and it solved the problem :) :)

Thanks !
Topic archived. No new replies allowed.