Note: The following applies equally to Boost.Thread and C++11 threads.
I have a condition variable which condition is actually a simple boolean variable.
// assume these are global
mutex m;
condition_variable c;
boolean b = false;
I wanted to use the wait(lock, predicate)
syntax. I could use for example a lambda:
c.wait(lock, [] () { return b; });
But I supposed there should be a more idiomatic way to wrap a variable as a callable. So I found out that reference_wrapper
provides an operator()
that retrieves the wrapped value. Therefore I tried:
c.wait(lock, cref(b));
But g++ (4.9.1) does not compile it, arguing no matching function for call to '(boost::reference_wrapper<const bool>) ()'
(if I use std::ref
the error is somewhat different, but still not compiling).
Shouldn't a reference_wrapper
qualify as a proper predicate for a condition variable? If not, why? And what would be the right wrapper for b
in this case?
EDIT: So @Praetorian has explained quite right my mistake, but, is there really nothing like this on Boost or the standard (there may be mistakes here):
template<typename T>
struct as_callable {
T &objref;
as_callable(T &r) : objref(r) {}
T &operator()() {
return objref;
}
};