消费者生产者 C# 实现,具有 1 个定时消费者,用于批量发送算法
我需要实现一个模块,该模块可以对字典进行多个输入(多个线程写入字典)和一个定时使用者,该使用者获取该字典,使用某些 ISender 将其发送出去,并清除字典以获取新的大量数据。 问题是我需要设计我的互锁,这样消费线程可以最快地获取批量快照,同时允许生产线程继续写入新的已清除字典。 您建议使用互锁和 ConcurrentDictionary 的最佳消费者生产者设计是什么?
此致!
I need to implement a module that can have multiple inputs to a dictionary (multiple threads writing to a dictionary) and 1 timed consumer that takes this dictionary, sends it away using some ISender and clears the dictionary for a new bulk of data.
the problem is that i need to design my interlocks that way that the consuming thread takes the quickest snapshot of the bulk while allowing the producing threads to keep writing to a new cleared dictionary.
what is the best consumer producer design you would suggest using interlocks and ConcurrentDictionary?
Best Regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要让生产者线程直接将数据放入字典中。让他们将其放入某个线程安全队列中,例如BlockingCollection。然后,您的消费者线程可以从队列中获取项目,构建字典并将其发送出去,所有这些都不会阻塞生产者线程。
本质上完成了相同的工作,但以避免大部分阻塞的方式“分散”。
如果您特别担心单个队列上的争用,您甚至可以为每个生产者线程拥有一个单独的 BlockingCollection,然后在使用者中使用 BlockingCollection.TakeFromAny。
当然,问题是您的消费者线程除了简单地写入字典之外还执行其他操作。例如,如果他们需要检查给定的键是否已存在于字典中,那么这种设计突然变得更加复杂。
Don't let the producer threads put the data in the dictionary directly. Let them put it in some thread-safe queue, such as
BlockingCollection
. Your consumer thread can then take items from the queue, build the dictionary and send it away, all without blocking the producer threads.Essentially the same work gets done, but is "spread around" in a way that avoids most of the blocking.
If you are extra-worried about contention on that single queue, you can even have a separate
BlockingCollection
per producer thread and then useBlockingCollection.TakeFromAny
in the consumer.The problem is, of course, if your consumer threads do anything other than simply writing to the dictionary. If they need, for example, to check if the given key already exists in the dictionary, then this design suddenly becomes much more complicated.
我能想到的最快的方法是使用多个字典对象。
当您的使用者线程运行时,它会创建一个新的 ConcurrentDictionary 并将其设置为“实时”字典。这是快速的,意味着生产者可以在最小的干扰下继续进行。
消费者线程现在“拥有”先前的字典对象,并且可以在自己的时间处理其内容。
The fastest way I can think of is to use multiple dictionary objects.
When your consumer thread runs, it creates a new
ConcurrentDictionary
and sets it as the "live" dictionary. This is fast and means the producers can carry on with minimal interruption.The consumer thread now "owns" the previous dictionary object and can process its contents in its own time.