4 ms·
> In general, MPSC queues are super fast and there's no real reason to prefer a locked queue. There's one significant advantage that a locked vector or deque h
by adwn 3y ago
> In general, MPSC queues are super fast and there's no real reason to prefer a locked queue.
There's one significant advantage that a locked vector or deque has over MPSC/MPMC queues: the consumers can dequeue all messages in a single operation by locking the vector, swapping it with an empty vector (typically, that's just 3 words), and locking it again. That's such a simple operation that it will typically be as fast or even faster than a single pop-one-message operation in an MPSC/MPMP. Similarly, if the vector is empty, a producer can push any number of messages in a single, constant-time operation.
- loeg 3y agoAll true, although I would quibble with: > That's such a simple operation that it will typically be as fast or even faster than a single pop-one-message operation in an MPSC/MPMP. Not if the lock is blocked because one of the writers context switched out! The typical case is good, but the worst case is pretty bad.