schets / multiqueue

A fast mpmc queue with broadcast capabilities
MIT License
201 stars 29 forks source link

Clarification on the nature of "starving" #18

Open dylanede opened 7 years ago

dylanede commented 7 years ago

The examples mention that unsubscribing from certain streams is necessary in order to stop other streams from "starving". It would be helpful if the documentation could explain what the conditions that lead to starvation are, as there doesn't appear to be any other mention of it.

unrealhoang commented 7 years ago

As I understand, in the current API:

let (send, recv) = multiqueue::broadcast_queue(4);
for i in 0..2 { // or n
    let cur_recv = recv.add_stream();
    thread::spawn(move || {
        for val in cur_recv {
            println!("Stream {} got {}", i, val);
        }
    });
}

The recv itself is a stream - a reader with cursor into the queue (each cur_recv in the loop is also an another stream), so if recv not moving its cursor (by reading/iterating), the queue will not accept more message if it is already at its limit, thus, prevent producer from producing.

And @schets addressed this API problem in this: https://github.com/schets/multiqueue/issues/15

schets commented 6 years ago

The issue is caused since multiqueue currently stalls the producer when a consumer is behind. I'm looking into an api that allows consumers to remain unmonitored by the producer and error when they are overwritten. I'll update the docs with a good example when I have some time