我目前正在尝试编写并发队列,但我有一些段错误,我无法向自己解释。我的队列实现本质上是由本网站上的第一个列表给出的。并发队列中的竞争条件
该网站说,有一个竞争条件,如果对象是并行地从队列中删除,但我只是不明白为什么还有一个,任何人都可以解释给我吗?
编辑:这是代码:
template<typename Data>
class concurrent_queue
{
private:
std::queue<Data> the_queue;
mutable boost::mutex the_mutex;
public:
void push(const Data& data)
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.push(data);
}
bool empty() const
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.empty();
}
Data& front()
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.front();
}
Data const& front() const
{
boost::mutex::scoped_lock lock(the_mutex);
return the_queue.front();
}
void pop()
{
boost::mutex::scoped_lock lock(the_mutex);
the_queue.pop();
}
};
那里有一个以上的代码片段,他们说哪一个代码片段有比赛? – hmatar
...细节在这里非常重要。请发布您正在使用的实际代码。 – Mat
他的意思是实现*阻塞*并发队列。如果队列在使用时试图从中弹出项目,那该怎么办? – Nawaz