结构单一生产者多个消费者 [英] architecture for single producer multiple consumers

查看:114
本文介绍了结构单一生产者多个消费者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设计的GUI来显示市场数据的过程。目前,我有多个窗户,消耗同样的数据,但显示的是不同方面。我试图找出建筑师这个系统的最佳方式。

I am in the process of designing a GUI to display market data. Currently, I have multiple windows that consume the same data but display different aspects of it. I am trying to figure out the best way to architect this system.

我有市场数据和多个消费者其注册生产者回调的一个生产者。当制片方愿与数据,它通过消费者列表进行迭代,并且通过回调到每个消费者分发数据。我不知道这是否是来分发数据的最佳方法。每个消费者必须等待的previous消费者完成处理回调,才可以得到其数据。

I have a single producer of market data and multiple consumers which register a callback with the producer. When the producer is ready with data, it iterates through a list of consumers and distributes data to each consumer via the callback. I'm not sure if this is the best method to distribute data. Each consumer has to wait for the previous consumer to finish processing the callback before it can get its data.

有没有办法通过,所有消费者可以在同一时间或以最小的延迟得到的数据?我使用C#4.0,并想知道是否有任何语言功能,允许这一点。

Is there a way by which all consumers can get data at the same time or with minimal delays ? I am using C# 4.0 and would like to know if there are any language features which enable this.

推荐答案

您可以通过使用一个单独的 BlockingCollection 每消费

You could go for a fairly simple solution by using a separate BlockingCollection per consumer.

生产者线程只需要一个列表与LT;> BlockingCollection 它管理自己的队列。当生产者线程产生一个项目,它只是需要将它添加到的使用每个的队列列表中的 BlockingCollection.Add()。注:这是同一项目的排队多次 - 一旦每个消费者队列

The producer thread just needs a List<> of BlockingCollection queues that it manages itself. When the producer thread produces an item, it just needs to add it to each queue in the list using BlockingCollection.Add(). Note: That is the same item being queued multiple times - once to each consumer queue.

项目类型应该是不可变的引用类型 - 那么它是安全的消费者分享和开销只是每个消费者队列参考

The item type should be an immutable reference type - then it is safe to share among the consumers AND the overhead is just a reference in each consumer queue.

这是不是最有效的途径,因为它需要每一个消费者的队列,但我不认为这是太糟糕了,除非队列为大。这是很容易理解的,虽然实现。

This isn't the most efficient route since it requires a queue per consumer, but I don't think it's too bad unless the queues are big. It's very easy to understand and implement though.

A BlockingCollection 使得它很容易告诉消费者线程时,有没有更多的数据,使他们可以完全退出。

A BlockingCollection makes it very easy to tell the consumer threads when there is no more data so that they can exit cleanly.

生产者线程调用 BlockingCollection.CompleteAdding() 发信号给消费线程时,有没有更多的数据,他们应该退出。

The producer thread calls BlockingCollection.CompleteAdding() to signal to the consuming thread that they should exit when there is no more data.

同时,所有的消费线程需要做的就是这个(假设队列是他们BlockingCollection队列):

Meanwhile, all the consuming threads need to do is this (assuming queue is their BlockingCollection queue):

foreach (var item in queue.GetConsumingEnumerable())
{
    ... process item
}

就是这样。消费者将在的foreach 循环自动阻止时,有没有更多的数据,他们将自动退出的foreach 时没有更多的数据的的制片人已要求 CompleteAdding()

That's it. The consumers will automatically block in the foreach loop when there is no more data, and they will automatically exit the foreach when there is no more data and the producer has called CompleteAdding().

这篇关于结构单一生产者多个消费者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆