condition variable predicate and lambda
I want to do something simple, but implementing it seems awefully complex. All I want to do is wait for a specified amount of time for a member variable of my class to equal a certain value. Can someone please help me with an example? I've tried several that came from Googling and read the documentation, but I get compile errors no matter what I try. Currently the compiler is complainging "term does not evaluate to a function taking 0 arguments" Here is the problem in all its simplicity: class A { public: enum STATE { HAPPY = 0, SAD, ANGRY, NUM_STATES }; Foo(boost::posix_time::millisec & timeout) { // I need to wait to return from this until the state is HAPPY } private: STATE state_; };
On Tue, Oct 18, 2011 at 3:24 PM, Christopher Pisz
I want to do something simple, but implementing it seems awfully complex.
You've left a lot of the requirements unstated, but you seem to be talking about cross-thread communication, and that domain is fraught with peril. It's inherently complex.
All I want to do is wait for a specified amount of time for a member variable of my class to equal a certain value.
So, is some other thread going to set it to that value? If not, we could wait a long time... Apparently you'd need to protect both the 'set' access and the testing access. You also seem to be stating two different behaviors in the sentence above: - Do not return until the variable has been set as desired. - Do not wait longer than a specified timeout. (Then what? Exception? Return a flag value?) I haven't messed with cross-thread communication myself, so any example code I propose is quite likely to be horribly wrong. I'm merely suggesting that your initial mail may not be clear enough for people who understand this domain to give you a solid answer.
All I want to do is wait for a specified amount of time for a member variable of my class to equal a certain value.
Can someone please help me with an example?
I've tried several that came from Googling and read the documentation, but I get compile errors no matter what I try.
Currently the compiler is complainging "term does not evaluate to a function taking 0 arguments"
Here is the problem in all its simplicity:
class A { public:
enum STATE { HAPPY = 0, SAD, ANGRY, NUM_STATES };
Foo(boost::posix_time::millisec & timeout) { // I need to wait to return from this until the state is HAPPY
}
private: STATE state_; };
{ unique_lock lock(mutex_); condvar_->timed_wait(lock, timeout, lambda::var(state_) == HAPPY); }
participants (3)
-
Christopher Pisz
-
Igor R
-
Nat Linden