我想改变我看到here的线程安全循环缓冲区, 在条件变量中添加谓词,如此article。
我需要在缓冲区空了很多时间后退出我的等待状态....所以这是我的循环缓冲区模板快照:
//Template of CIRCULAR BUFFER thread-safe
template <typename T>
class circ_buffer : private boost::noncopyable{
public:
typedef boost::mutex::scoped_lock lock;
circ_buffer(){}
circ_buffer(int n){cb.set_capacity(n);}
........
struct cb_not_empty{
boost::circular_buffer<T>& circ_buf;
cb_not_empty(boost::circular_buffer<T>& circular_buffer_):
circ_buf(circular_buffer_){}
bool operator ()() const{
return !circ_buf.empty();
}
};
template<typename Duration>
bool timed_wait_and_pop(T& popped_valued,Duration const& wait_duration){
lock lk(monitor);
if(!buffer_not_empty.timed_wait(lk,wait_duration,cb_not_empty(the_cb)))
return false;
popped_valued = the_cb.front();
the_cb.pop();
return true;
}
.......
private:
boost::condition buffer_not_empty;
boost::mutex monitor;
boost::circular_buffer<T> cb;
std::atomic<bool> circ_buf_flag; //flag to turn ON/OFF circular buffer: make circ_buf OFF to go out
};
我的gnu编译器给我
error: 'the_cb' was not declared in this scope if(!buffer_not_empty.timed_wait(lk,wait_duration,cb_not_empty(the_cb))) ^