我需要在C ++中使用具有超时功能offer()
的阻塞队列。该队列适用于多个生产者,一个消费者。回到我实施的时候,我没有找到任何符合这种需求的好的现有队列,所以我自己编写了它。
我看到段错误来自队列中的take()
方法,但它们是间歇性的。我一直在寻找问题的代码,但我没有看到任何看起来有问题的东西。
我想知道是否:
以下是标题:
class BlockingQueue
{
public:
BlockingQueue(unsigned int capacity) : capacity(capacity) { };
bool offer(const MyType & myType, unsigned int timeoutMillis);
MyType take();
void put(const MyType & myType);
unsigned int getCapacity();
unsigned int getCount();
private:
std::deque<MyType> queue;
unsigned int capacity;
};
相关实施:
boost::condition_variable cond;
boost::mutex mut;
bool BlockingQueue::offer(const MyType & myType, unsigned int timeoutMillis)
{
Timer timer;
// boost::unique_lock is a scoped lock - its destructor will call unlock().
// So no need for us to make that call here.
boost::unique_lock<boost::mutex> lock(mut);
// We use a while loop here because the monitor may have woken up because
// another producer did a PulseAll. In that case, the queue may not have
// room, so we need to re-check and re-wait if that is the case.
// We use an external stopwatch to stop the madness if we have taken too long.
while (queue.size() >= this->capacity)
{
int monitorTimeout = timeoutMillis - ((unsigned int) timer.getElapsedMilliSeconds());
if (monitorTimeout <= 0)
{
return false;
}
if (!cond.timed_wait(lock, boost::posix_time::milliseconds(timeoutMillis)))
{
return false;
}
}
cond.notify_all();
queue.push_back(myType);
return true;
}
void BlockingQueue::put(const MyType & myType)
{
// boost::unique_lock is a scoped lock - its destructor will call unlock().
// So no need for us to make that call here.
boost::unique_lock<boost::mutex> lock(mut);
// We use a while loop here because the monitor may have woken up because
// another producer did a PulseAll. In that case, the queue may not have
// room, so we need to re-check and re-wait if that is the case.
// We use an external stopwatch to stop the madness if we have taken too long.
while (queue.size() >= this->capacity)
{
cond.wait(lock);
}
cond.notify_all();
queue.push_back(myType);
}
MyType BlockingQueue::take()
{
// boost::unique_lock is a scoped lock - its destructor will call unlock().
// So no need for us to make that call here.
boost::unique_lock<boost::mutex> lock(mut);
while (queue.size() == 0)
{
cond.wait(lock);
}
cond.notify_one();
MyType myType = this->queue.front();
this->queue.pop_front();
return myType;
}
unsigned int BlockingQueue::getCapacity()
{
return this->capacity;
}
unsigned int BlockingQueue::getCount()
{
return this->queue.size();
}
是的,我没有使用模板实现类 - 这是列表中的下一个:)
非常感谢任何帮助。线程问题可能很难确定。
-Ben
答案 0 :(得分:0)
为什么是cond和mut globals?我希望它们是你的BlockingQueue对象的成员。我不知道还有什么东西在触及那些东西,但那里可能存在问题。
我也将ThreadSafeQueue实现为更大项目的一部分:
https://github.com/cdesjardins/QueuePtr/blob/master/include/ThreadSafeQueue.h
这与你的概念类似,除了enqueue(aka offer)函数是非阻塞的,因为基本上没有最大容量。为了强制执行容量,我通常有一个池,在系统初始化时添加N个缓冲区,在运行时添加消息队列,这也消除了在运行时分配内存的需要,我认为这是一件好事(我通常从事嵌入式应用程序。)
池和队列之间的唯一区别是池在系统初始化时获得了一堆缓冲区。所以你有这样的事情:
ThreadSafeQueue<BufferDataType*> pool;
ThreadSafeQueue<BufferDataType*> queue;
void init()
{
for (int i = 0; i < NUM_BUFS; i++)
{
pool.enqueue(new BufferDataType);
}
}
然后,当您想要发送消息时,您可以执行以下操作:
void producerA()
{
BufferDataType *buf;
if (pool.waitDequeue(buf, timeout) == true)
{
initBufWithMyData(buf);
queue.enqueue(buf);
}
}
这样enqueue函数既快速又简单,但如果池为空,那么您将阻塞,直到有人将缓冲区放回池中。这个想法是一些其他线程将在队列上阻塞,并且当它们按照如下处理时将缓冲区返回到池中:
void consumer()
{
BufferDataType *buf;
if (queue.waitDequeue(buf, timeout) == true)
{
processBufferData(buf);
pool.enqueue(buf);
}
}
无论如何看看它,也许它会有所帮助。
答案 1 :(得分:0)
我认为代码中的问题是通过多个线程修改deque。看:
因此,在修改双端队列后尝试放置所有cond.notify_*()
。即:
void BlockingQueue::put(const MyType & myType)
{
boost::unique_lock<boost::mutex> lock(mut);
while (queue.size() >= this->capacity)
{
cond.wait(lock);
}
queue.push_back(myType); // <- modify first
cond.notify_all(); // <- then say to others that deque is free
}
为了更好地理解,我建议您阅读pthread_cond_wait()
。