โ† Back to the chapter

Technical detail ยท Message Queue Fundamentals

How a circular buffer tells full from empty

A circular buffer uses a fixed array. The head points to the next item to remove; the tail points to the next slot to fill. Both wrap to the start at the end of the array, preserving FIFO removal without an ever-growing storage area.

In a design using every slot, equal pointers can mean either empty or full. Keep an explicit count or full flag to distinguish those states. Equal pointers with a count of zero mean empty; a count equal to capacity means full. Assuming empty could overwrite unread messages, while assuming full could reject work despite free space. Pointer equality is a normal condition, not evidence of corruption.

Another design reserves one unused slot. Equal pointers then mean empty, and a next write position that would reach the head marks full. Both approaches work when the implementation consistently follows its chosen rule. The storage mechanism must preserve the capacity and ordering promised by the queue contract.

โ† Back to the chapter