Real Time Operating System
“FreeRTOS”
Queue
1
Agenda
• How to create a queue.
• How a queue manages the data it contains.
• How to send data to a queue.
• How to receive data from a queue.
• What it means to block on a queue.
• The effect of task priorities when writing to
and reading from a queue.
• Only task-to-task communication is covered
What is Queue?
• A queue can hold a finite number of fixed size data items. The
maximum number of items a queue can hold is called its ‘length’.
• Both the length and the size of each data item are set when the
queue is created.
• Normally, queues are used as First In First Out (FIFO) buffers
where data is written to the end (tail) of the queue and removed
from the front (head) of the queue.
• It is also possible to write to the front of a queue.
• Writing data to a queue causes a byte-for-byte copy of the data to
be stored in the queue itself.
• Reading data from a queue causes the copy of the data to be
removed from the queue.
What is Queue?
Blocking on Queue Reads
• When a task attempts to read from a queue it can optionally specify a ‘block’ time.
• This is the time the task should be kept in the Blocked state to wait for data to be
available from the queue should the queue already be empty.
• A task that is in the Blocked state, waiting for data to become available from a
queue, is automatically moved to the Ready state when another task or interrupt
places data into the queue.
• The task will also be moved automatically from the Blocked state to the Ready state
if the specified block time expires before data becomes available.
• Queues can have multiple readers so it is possible for a single queue to have more
than one task blocked on it waiting for data.
• Only one task will be unblocked when data becomes available. The task that is
unblocked will always be the highest priority task that is waiting for data.
• If the blocked tasks have equal priority, then the task that has been waiting for data
the longest will be unblocked.
Blocking on Queue Writes
• A task can optionally specify a block time when writing to a queue.
• The block time is the maximum time the task should be held in the
Blocked state to wait for space to become available on the queue,
should the queue already be full.
• Queues can have multiple writers, so it is possible for a full queue
to have more than one task blocked on it waiting to complete a
send operation.
• Only one task will be unblocked when space on the queue
becomes available.
• The task that is unblocked will always be the highest priority task
that is waiting for space.
• If the blocked tasks have equal priority, then the task that has
been waiting for space the longest will be unblocked.
Example 10. Blocking when receiving from a queue
Example 10. Blocking when receiving from a queue
Example 10. Blocking when receiving from a queue
Example 10. Blocking when receiving from a queue
Example 10. Blocking when receiving from a queue
Using Queues to Transfer Compound Types
• A task is to receive data from multiple sources on a single queue.
• The receiver of the data needs to know where the data came from
• Use the queue to transfer structures where both the value of the
data and the source of the data are contained
Example 11. Blocking when sending to a queue or sending
structures on a queue
Example 11. Blocking when sending to a queue or sending
structures on a queue
Example 11. Blocking when sending to a queue or sending
structures on a queue
Example 11. Blocking when sending to a queue or sending
structures on a queue
Example 11. Blocking when sending to a queue or sending
structures on a queue
Example 11. Blocking when sending to a queue or sending
structures on a queue
Example 11. Blocking when sending to a queue or sending
structures on a queue
Example 11. Blocking when sending to a queue or sending
structures on a queue