CUtilityCode
2016-12-19 14:21 钱吉 阅读(281) 评论(0) 编辑 收藏 举报(1) 基于boost的生产者/消费者队列
template<typenameData> classconcurrent_queue { private: std::queue<Data> the_queue; mutableboost::mutex the_mutex; boost::condition_variable the_condition_variable; public: void push(Data const& data) { boost::mutex::scoped_lock lock(the_mutex); the_queue.push(data); lock.unlock(); the_condition_variable.notify_one(); } bool empty() const { boost::mutex::scoped_lock lock(the_mutex); returnthe_queue.empty(); } bool try_pop(Data& popped_value) { boost::mutex::scoped_lock lock(the_mutex); if(the_queue.empty()) { returnfalse; } popped_value=the_queue.front(); the_queue.pop(); returntrue; } void wait_and_pop(Data& popped_value) { boost::mutex::scoped_lock lock(the_mutex); while(the_queue.empty()) { the_condition_variable.wait(lock); } popped_value=the_queue.front(); the_queue.pop(); } };