【发布时间】:2013-01-13 17:29:49
【问题描述】:
我目前正在尝试编写一个并发队列,但我有一些我无法向自己解释的段错误。我的队列实现基本上由本网站的第一个列表给出。
该网站说如果从队列中并行删除对象,则会出现竞争条件,但我只是不明白为什么会有一个,谁能向我解释一下?
编辑:这是代码:
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();
}
};
【问题讨论】:
-
那里有不止一个代码sn-ps,他们说哪个有比赛?
-
...细节在这里非常重要。请发布您正在使用的实际代码。
-
他的意思是实现阻塞并发队列。如果队列在他们尝试从中弹出项目时为空怎么办?
标签: c++ concurrency queue race-condition