Bill Li
Bill Li

Reputation: 678

How a consumer posts back to the same Disruptor ring buffer

In a multiple producer setup, there is one producer thread and one consumer thread. Can the consumer post new events back to the same ring buffer? I assume it breaks when the buffer is full and the consumer thread will never get a free slot while it is working on the current event. In other words, dead lock happens.

What is the best way to do this? Do I have to introduce a sort of proxy thread who receives events from the consumer and post them to the ring buffer like normal producers?

supplement - why is it useful? Say the consumer thread is processing stock market data event and it needs to send an order to a market simulator(a class), and the market simulator should send an order execution event to the same ring buffer, ideally.

Upvotes: 0

Views: 749

Answers (2)

Sam Turtel Barker
Sam Turtel Barker

Reputation: 843

Providing a separate answer in light of additional detail in question (I still believe my original answer to be valid).

In my head/experience you're trying to do to much in one go as your conflating the consumer of the ring buffer and the publisher which are usually separate concerns. I would generally expect the response events from your simulator to be delivered to the ring buffer in the same way as the original events its processing.

Having said that what you're asking for is actually possible.

You do however have to do a bit more of the work your self and write a custom EventProcessor which would allow you to mark the incoming message as processed (thus freeing the slot) before trying to publish an event back.

Upvotes: 1

Sam Turtel Barker
Sam Turtel Barker

Reputation: 843

If your using a Single producer setup then no the consumer can't post back to the ring buffer as it is not the Single producer.

Taking a step back why would one and only one consumer ever need to write to the ring buffer? It already knows what its done and nothing else will be reading from the buffer.

If you have multiple consumers you can write back to the buffer but make sure the additional consumers are gated on the consumer which is writing back to the buffer rather than the producer sequence.

Upvotes: 0

Related Questions