生产者/使用者,如何确保在关闭所有使用者之前耗尽线程安全队列
Producer/Consumer, How to make sure a thread safe queue is drained before turning off all consumers?
我正在使用线程安全队列处理一个项目。这基本上是一个生产者/消费者的问题。
我目前的代码是
void threadCode()//the consumer
{
while(active) // active is an atomic int, we use it to turn off everything during destruction
{
threadSafeQueue.popFront();//if queue is empty, it will wait for a signal.The queue has a CV.
// process it
// if it fails to process it but less than three times,
// put it back to the queue to retry later
}
}
问题是,当我的析构函数将 active 更改为 0 时,即使队列不为空,所有线程都将终止。例如,它无法处理项目,将其放回队列,然后活动现在为 0。
我不希望这种情况发生。我希望在处理队列中的所有内容后销毁实例。
所以我试了这个,
void threadCode()
{
while( active || queue.size() != 0 )
{ //[1]
queue.popFront();
//process
// put it back to the queue if it fails less than 3 times
}
}
queue.size() 和 queue.popFront() 是线程安全的。但将它们放在一起并不是...如果队列中只剩下一件事,并且上下文切换发生在 [1]。该线程可能会永远沉睡。
因为我在析构函数中有类似 threadpool.join() 的东西,并且该线程永远不会唤醒。问题就卡在那里了。
我想知道有没有人有更好的主意来解决这个问题?
谢谢!!
不要让使用者线程检查外部标志,而是让队列本身维护一个内部"关闭"标志。 如果没有更多要处理的工作,则 .popFront()
函数返回"正在关闭"状态,而不是要处理的项目。
相关文章:
- boost::进程间消息队列引发错误
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- Android NDK传感器向事件队列报告奇怪的间隔
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 在一个读写器队列中,我可以用volatile替换原子吗
- 为什么我的多线程作业队列崩溃
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 使用"Task"函数指针队列定义作业管理器
- 在c++队列中使用pop和visit实现线程安全
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 打印优先级队列
- 共享队列的线程安全
- 带自定义比较器的最小优先级队列
- 在 Vulkan Qt 中获取队列系列
- 堆栈和队列是否像C++中的数组一样传递?
- 生产者/使用者,如何确保在关闭所有使用者之前耗尽线程安全队列
- C++11 使用 std::atomic(多写入器,单使用者)的无锁队列