I have exactly 100 Sensors each “measuring” own data. I have exactly one DataSender which should send information from “sensors”. The most recent information should be sent.
Bandwidth of the channel may be less than data produced by 100 sensors. In this case some data can be skipped – but we should be “roughly fair”. For example, we could skip every second measurement from each sensor.
I don’t know how often each sensor generates data, but in general they generate data pretty often.
After my other posts:
- how to create singleton which always running in separate thread?
- Modified Producer/Consumer example, any problems with it?
I have decided that I have classical Producer/Consumer problem, with:
- 100 Producers, and
- 1 Consumer
I’ve been suggested to use BlockingCollection for this. The only problem with BlockingCollection – once you have added item, you cannot replace it. But in my application, if sensor produces a new value, and previous value was not processed by the Consumer, the value should be replaced.
Should I use use a ConcurentDictionary or ConcurentBag for that task?
Conceptually, all I need is an array of 100 elements.
Sensor #33 should replace it’s value into array[33]:
| Sensor | Value |
|--------|-------|
| 1 | |
| 2 | |
| 3 | |
/......../......./
| 32 | |
| 33 | 101.9 |
| 34 | |
/......../......./
| 98 | |
| 99 | |
| 100 | |
Consumer should take value from array[33] and if not null, then send it and set array[33] to null. Consumer should react on any not null values in array asap.
I think you should implement your own
IProducerConsumerCollection<T>. That’s why it’s an interface: so that you could easily make your own.You could do it using
Dictionary<K,V>andQueue<T>to make sure receiving the data is fair, i.e. if you have just one device that produces data very fast, you won’t send data just from this one.When used along these lines:
it produces the following output: