such a SPSC queue does not need mutex or futex at all. It's a matter
of taste the policy you use to handle the consumer dequeue() when the
queue is empty.
You are using the linux syscall futex as signaling mechanism, but it
is also ok to use a std::condition_variable or a more aggressive
policy like std::this_thread::yield().
The important point to understand is that such an event does not occur
at any dequeue, but only when the queue is empty, which should happen
with a low frequency (unless the producer is very slow, and in that
case it's much better to reschedule the consumer that polling the
empty queue).
If the consumer has a reserved core, by means of affinity, probably
it's better to yield and poll again, but yet it depends much on the
application.
Ciao,
Nicola
--
The difference between theory and practice is bigger in practice than in theory.
Sent from a phone
I can tell you that for this queue the address passed to the futex
(&tail->next_) is an invariant with respect to the enqueue() member
function.
For this reason it seems that your considerations are good, even if
other pitfalls may be waiting in ambush :-)
For instance what happens if the producer pushes an element and wakes
the consumer in the middle between it finds the queue empty and waits?
Does the FUTEX_WAIT exits immediately if the initial condition is not
met ?
Ciao,
Nicola
--