Full name: Queue and first in, first out (FIFO).
Definition
A queue is a container that holds items waiting to be processed. Enqueue adds an item at the back; dequeue removes the item at the front. FIFO (first in, first out) is the rule that the item that has waited longest leaves first, so items leave in the order they were added; a queue that follows it is a FIFO queue. A stack, which removes the newest item first, and a priority queue §14.16, which removes the most urgent item first, follow different rules.
The idea
A sensor driver produces readings in bursts, and a slower task processes them one at a time. A queue sits between them. The producer, the code that creates items, enqueues each reading; the consumer, the code that processes items, dequeues readings when it is ready. FIFO order means that the readings are processed in the order they were taken.
The picture is a line of people at a counter: newcomers join at the back, and the front person is served next. The picture fails in three places. People arriving at the same moment end up in an order none of them chooses, and so do items enqueued at the same moment. With several counters, people leave the line in order but finish in any order. And a priority rule lets an urgent item pass everyone.
How it works
The interface has these operations:
enqueue(x): add the itemxat the back;dequeue(): remove and return the front item, or report that the queue is empty;peek(): return the front item without removing it;size(): the number of items held.
A bounded queue §9.31, one with a fixed capacity, must also say what enqueue does when the queue is full: refuse the item, discard an item, or make the producer wait, which slows it to the consumer’s pace and is a form of backpressure §9.30.
Three implementations are common.
A linked list §14.6 with pointers to its first and last nodes: enqueue links a node after the last, and dequeue unlinks the first.
A ring buffer §14.15: a fixed array §14.5 whose read and write positions wrap around from its end to its start; it allocates no memory per item, which suits embedded software.
Two stacks: enqueue pushes onto an input stack; dequeue pops from an output stack, first refilling it, when empty, with every item of the input stack, which reverses their order and puts the oldest on top.
Each makes enqueue and dequeue take constant time, time that does not grow with the number of items; for two stacks only on average over a sequence of operations, as amortized analysis §14.4 shows.
FIFO order is set by the moment each enqueue takes effect inside the queue, not by when a producer created the item or read a clock. With one producer, that is the producer’s own order. When the enqueue calls of several producers overlap in time, a concurrent queue picks the order. A linearizable queue behaves as if each operation took effect at one instant between its call and its return, its linearization point §8.6, and the operations ran one at a time in the order of those instants. So an enqueue that returns before another is called puts its item first; overlapping calls may take effect in either order. Each producer’s items therefore leave in that producer’s order, interleaved with other producers’ items in an order no producer controls.
A priority queue removes items by urgency, so a late urgent item leaves before an early ordinary one, and only a tie rule orders equal priorities. Several consumers dequeue in FIFO order but take different times, so items can finish in any order. Three orders must be kept apart: submission order, in which producers create items; dequeue order, in which the queue releases them; and completion order, in which their processing ends.
Worked example
Two producers. Producer P enqueues P1 and then P2; producer Q enqueues Q1 and then Q2; the calls of P and Q overlap in time. A linearizable FIFO queue keeps P1 before P2 and Q1 before Q2. Choosing which 2 of the 4 places in the output hold P’s items fixes the whole order, so orders are possible, depending on timing:
P1 P2 Q1 Q2,P1 Q1 P2 Q2,P1 Q1 Q2 P2,
Q1 P1 P2 Q2,Q1 P1 Q2 P2,Q1 Q2 P1 P2.
The order P2 P1 Q1 Q2 is impossible, because P calls enqueue for P2 only after its call for P1 has returned.
Two consumers. Item A is enqueued before item B, and two workers serve the queue. Worker 1 dequeues A at time 0 milliseconds (ms) and needs 100 ms, so A completes at ms. Worker 2 dequeues B at 1 ms and needs 1 ms, so B completes at ms. The dequeue order was A then B, as FIFO requires, yet B completed ms before A.
A priority rule. A priority queue receives A with priority 3 and then B with priority 1, where a smaller number means more urgent. The consumer receives B first. A FIFO queue would have returned A first.
Stated precisely
With one operation at a time, the state of a FIFO queue is a sequence of items in which is the oldest. Enqueueing an item changes the state to . Dequeueing from a queue with returns and leaves ; with it reports that the queue is empty, or waits, as the interface states. Consequence: if item is enqueued before item and both are dequeued, is dequeued first.
For a concurrent queue, “ is enqueued before ” means that the linearization point of the enqueue of comes first. That is guaranteed when the enqueue of returns before the enqueue of is called, and so for any two items of one producer. The contract covers dequeue order only, not when processing runs or completes. A queue that drops items under overload keeps FIFO order only among the items it keeps, so each drop must be reported.
Limits and common mistakes
FIFO does not bound waiting time. An item that finds items ahead of it waits until all have been dequeued, so its wait grows with the length of the queue; the queueing delay §14.38 and Little’s law §14.39 entries treat this. An unbounded queue under sustained overload grows until memory runs out.
FIFO does not make producers fair. A fast producer can fill the queue so that each item of a slow producer waits behind many items of the fast one.
Timestamps that producers take before calling enqueue do not give the queue order. A producer can be interrupted between reading the clock and enqueueing, so an item with a later timestamp can leave first.
Some queues promise less than strict FIFO: a distributed message service may keep order only within one partition of its data, and some fast concurrent queues let items leave slightly out of order.
Retrying a failed item by enqueueing it again puts it behind items that arrived after it.
Questions answered
Can priority or multiple producers change the order seen by a consumer?
Yes: a priority rule and multiple producers can both change the order in which a consumer receives items. A priority queue removes the most urgent item first, so an urgent item added late overtakes an ordinary one added early; only an explicit tie rule orders equal priorities. With several producers, a FIFO (first in, first out) queue releases items in the order in which their enqueue operations took effect inside the queue. A linearizable queue, one that acts as if each operation happened at one instant between its call and its return, keeps each producer’s items in that producer’s order but interleaves different producers’ items by timing, not by the producers’ timestamps. Several consumers add a third effect: items leave in FIFO order but can finish in any order. State which order the application needs: submission, dequeue or completion order.
Related entries: Ring buffer (§14.15), Priority queue (§14.16), Backpressure (§9.30), Bounded queue (§9.31), Linearization point (§8.6).