简体   繁体   English

当生产者也是消费者时,如何在生产者/消费者模式中使用阻塞收集 - 我该如何结束?

[英]How do I use a blockingcollection in the Producer/Consumer pattern when the producers are also the consumers - How do I end?

I have a recursive problem where The consumers do some work at each level of a tree, then need to recurse down the tree and perform that same work at the next level. 我有一个递归问题,消费者在树的每个级别做一些工作,然后需要递归树并在下一级执行相同的工作。

I want to use ConcurrentBag/BlockingCollection etc to run this in parallel. 我想使用ConcurrentBag / BlockingCollection等并行运行它。 In this scenario, the consumers of the queue, are also the producers for the queue! 在这种情况下,队列的使用者也是队列的生产者!

My problem is this : Using BlockingCollection, I can write very simple foreach logic to dequeue items, and queue new ones - When the queue is empty, the blocking collection will block correctly, and wait for new work to be produced by one of the other consumers. 我的问题是这样的:使用BlockingCollection,我可以编写非常简单的foreach逻辑来对项目进行队列化,并对新项目进行排队 - 当队列为空时,阻塞集合将正确阻塞,并等待其中一个新工作生成消费者。

But how do I know if all of the consumers are blocking?! 但我如何知道所有消费者是否都在阻止?!

I know about CompleteAdding(), but that does not seem to serve, as the only time you are actually complete is when all of the producers are done producing and the queue is empty - and since they would all be blocking, there is nobody "free" to set CompleteAdding(). 我知道CompleteAdding(),但这似乎没有服务,因为你实际完成的唯一时间是所有的生产者都完成生产并且队列是空的 - 并且因为它们都会阻塞,所以没有人“免费“设置CompleteAdding()。 Is there a way to detect this? 有没有办法检测到这个? (Perhaps an event that can fire when blocking, and fire again when unblocked?) (也许是一个可以在阻挡时触发的事件,并在解锁时再次触发?)

I can deal with this manually, by not using a foreach, but manually having a while(!complete) loop, and using TryTake, but then I need to manually sleep, which seems inefficent (the whole reason to have the blocking collection vs just the concurrent collections in the first place!) Each time through the loop, if TryTake is false, I could set an Idle flag, and then have a Master check if the queue is empty, and all threads are idle, set a complete flag, but again, this seems kludgy. 我可以手动处理,不使用foreach,但手动有一段时间(!完整)循环,并使用TryTake,但后来我需要手动睡眠,这似乎是无效的(完全的原因是阻止集合vs只是首先是并发集合!)每次循环,如果TryTake为假,我可以设置一个空闲标志,然后有一个Master检查队列是否为空,并且所有线程都空闲,设置一个完整的标志,但同样,这似乎很糟糕。

Intuition is telling me there is some way to use Blocking Collection to do this, but I cant quite get there. Intuition告诉我有一些方法可以使用Blocking Collection来做到这一点,但我不能完全达到目的。

Anyway, anyone have a good pattern for when the consumers are the producers and being able to detect when to release all the blocks would be awesome 无论如何,任何人都有一个良好的模式,当消费者是生产者,并能够检测何时释放所有块将是真棒

I think this link from MSDN can help you. 我认为来自MSDN的这个链接可以帮到你。

Reusable Parallel Data Structures and Algorithms 可重用的并行数据结构和算法

It exposes how to deal with some data structures in concurrent scenarios. 它揭示了如何在并发场景中处理一些数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用BlockingCollection <>解决生产者/消费者竞争条件 - How to solve producer/consumer race condition with BlockingCollection<> 如何实现BlockingCollection来修复此生产者/消费者问题? - How to implement BlockingCollection to fix this Producer/Consumer issue? 如何实现自己的高级生产者/消费者方案? - How do I implement my own advanced Producer/Consumer scenario? 生产者-消费者模式是否需要两个队列? - Do I need two queues for producer-consumer pattern? 如何使用RX执行正确的生产者-消费者模式 - How to do proper Producer-Consumer pattern with RX 生产者消费者模式您如何通知完成? - Producer Consumer Pattern How do you notify completion? 我如何使用NServiceBus来做竞争消费者 - How can i use NServiceBus to do Competing Consumers 使用阻塞收集和任务的经典生产者消费者模式 .net 4 TPL - classic producer consumer pattern using blockingcollection and tasks .net 4 TPL 使用 BlockingCollection 通过生产者-消费者模式保存图像 - Saving images via producer-consumer pattern using BlockingCollection 当任何生产者可以随时关闭整个通道时,通道消费者如何从多个通道生产者那里获得一切? - How does a channel consumer get everything from multiple channel producers when any producer can close the entire channel at any time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM