您的位置:首页 > 编程语言 > C语言/C++

C++线程安全队列

2016-05-31 13:25 381 查看
template<typename Data>
class concurrent_queue
{
private:
std::queue<Data> the_queue;
mutable boost::mutex the_mutex;
boost::condition_variable the_condition_variable;

public:
void push(Data const& data)
{
#if 0
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
lock.unlock();
#else   //利用作用域自动释放锁,防止异常崩溃场景
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
}
#endif
the_condition_variable.notify_one();
}

bool empty() const
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.empty();
}

//获取队列头部的值,并移除队列头部
bool try_pop(Data& popped_value)
{
boost::mutex::scoped_lock lock(the_mutex);
if(the_queue.empty())
{
return false;
}

popped_value=the_queue.front();
the_queue.pop();
return true;
}

//等待队列有值,再弹出队列的头部
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();
}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: