I have read your previous question "What type of IProducerConsumerCollection to use for my task?". If I understand it correctly, on one hand you want to process the data from sensors in approximate FIFO order, but on the other hand if data from some sensor weren't yet processed when newer data are ready, you want to update it discarding the older data.
I would suggest the following schema to you.
For the sensor data, simply use an array; you indicated that this is sufficient:
All I need is an array of 100 elements. Sensor #33 should REPLACE it's value into array[33] 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.
The trick is how you update the data. For that, use Interlocked.Exchange
instead of plain read/write. A producer uses it to write the new value, and then checks what was the old value (as returned by Interlocked.Exchange
). If not null, the old value was not read by the consumer, and you just successfully replaced it. If the old value was null, the previos data have been consumed; in this case, the producer sends a message to the consumer that new data are available. The message can consist of the number of the array cell to read, and can be sent through ConcurrentQueue
or another suitable mechanism.
The consumer waits for a message to arrive through that mechanism, finds the updated array cell number in the message, and also uses Interlocked.Exchange
to replace the current value in that cell with null, getting the value back as the result of the operation. It will always get the most recent value written into the cell, and it will process values in the order of messages being obtained; with ConcurrentQueue
, it should be FIFO.