jeudi 29 janvier 2015

Ring buffer, 1 writer and N readers

I need a ring buffer (or some other data structure) and an algorithm/pattern for handling the ring buffer in the following situation.


1 writer has to be always able to write into the first free/available "slot" (or wait until one slot is free for writing), and N concurrent readers should be always getting the most recent written data from one slot, but must not read the same data more than one time, and should be waiting if data has not been completely written in one slot yet since last read (think of a fast reader). Note that 1 reader does not "consume" the data for another reader.


Note that the disruptor might not be adequate for my case (or I just failed to make it work as I want). The problem with the disruptor is that the writer might advance so quickly (compared to slower readers) that it may end up overwriting some slot while they are in the process of being read. In this case, the writer should be able to skip these "busy" slots until it find the first free slot (once found, it also has to publish that slot only when it finishes to write), but the disruptor pattern does not seem to contemplate this situation. There is another issue with the sequence itself, which, in the disruptor implementation I am using, is an atomic integer, so it may overflow resulting in some undefined behaviour.


Do you have any idea? I'd aprreciate solutions in modern C++, if you know any.


Aucun commentaire:

Enregistrer un commentaire