Queues help tasks send and receive data safely. Handling full and empty queues prevents errors and keeps tasks running smoothly.
Queue full and empty handling in FreeRTOS
/* Queue creation */ QueueHandle_t xQueue = xQueueCreate( queue_length, item_size ); /* Sending to queue */ BaseType_t xQueueSend( QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait ); /* Receiving from queue */ BaseType_t xQueueReceive( QueueHandle_t xQueue, void * pvBuffer, TickType_t xTicksToWait );
xQueueSend returns pdTRUE if data was sent, or errQUEUE_FULL if the queue is full.
xQueueReceive returns pdTRUE if data was received, or pdFALSE if the queue is empty.
/* Example: Sending with zero wait time (non-blocking) */ if (xQueueSend(xQueue, &data, 0) != pdTRUE) { // Queue full, handle error }
/* Example: Receiving with wait time */ if (xQueueReceive(xQueue, &receivedData, pdMS_TO_TICKS(100)) == pdTRUE) { // Data received } else { // Queue empty after waiting 100 ms }
/* Edge case: Sending when queue is full */ BaseType_t result = xQueueSend(xQueue, &data, 0); if (result != pdTRUE) { // Handle full queue, e.g., discard or retry later }
/* Edge case: Receiving when queue is empty */ BaseType_t result = xQueueReceive(xQueue, &data, 0); if (result != pdTRUE) { // Handle empty queue, e.g., wait or skip }
This program creates a queue with space for 1 integer. It tries to receive from the empty queue first, then sends data, tries to send again when full, and receives twice. It prints messages showing full and empty queue handling.
#include "FreeRTOS.h" #include "queue.h" #include "task.h" #include <stdio.h> QueueHandle_t xQueue; void vSenderTask(void *pvParameters) { int dataToSend = 100; printf("Sender: Trying to send %d\n", dataToSend); if (xQueueSend(xQueue, &dataToSend, 0) == pdTRUE) { printf("Sender: Data sent successfully\n"); } else { printf("Sender: Queue full, cannot send data\n"); } vTaskDelete(NULL); } void vReceiverTask(void *pvParameters) { int receivedData = 0; printf("Receiver: Trying to receive data\n"); if (xQueueReceive(xQueue, &receivedData, 0) == pdTRUE) { printf("Receiver: Received data %d\n", receivedData); } else { printf("Receiver: Queue empty, no data to receive\n"); } vTaskDelete(NULL); } int main(void) { xQueue = xQueueCreate(1, sizeof(int)); if (xQueue == NULL) { printf("Failed to create queue\n"); return 1; } printf("Queue created with length 1\n"); // First, receive from empty queue vReceiverTask(NULL); // Then, send data vSenderTask(NULL); // Try sending again to full queue vSenderTask(NULL); // Receive data vReceiverTask(NULL); // Receive again from empty queue vReceiverTask(NULL); return 0; }
Time complexity for sending or receiving is O(1) because it just adds or removes one item.
Space complexity depends on queue length and item size.
Common mistake: Not checking return values, which can cause lost data or blocked tasks.
Use blocking waits if you want tasks to wait for space or data; use zero wait for non-blocking checks.
Always check if the queue is full before sending or empty before receiving.
Use the wait time parameter to control blocking behavior.
Proper handling keeps tasks responsive and prevents data loss.
Practice
Solution
Step 1: Understand queue full behavior
When a queue is full, FreeRTOS does not allow new items to be added without waiting or handling the error.Step 2: Check send operation without wait
If the send function is called with zero wait time and the queue is full, it returns immediately with an error indicating failure.Final Answer:
The send operation fails immediately and returns an error. -> Option AQuick Check:
Queue full + no wait = send fails [OK]
- Assuming queue grows automatically
- Thinking oldest data is overwritten
- Believing task suspends indefinitely
Solution
Step 1: Understand xQueueReceive parameters
The function takes the queue handle, a pointer to the variable to store the item, and the block time in ticks.Step 2: Identify correct syntax
Passing &item as the second argument is correct to provide the address. Block time 10 means wait up to 10 ticks.Final Answer:
xQueueReceive(queue, &item, 10); -> Option AQuick Check:
Pointer + block time 10 = correct receive call [OK]
- Passing item instead of &item
- Using zero or negative block time incorrectly
- Confusing send and receive parameters
QueueHandle_t queue = xQueueCreate(2, sizeof(int)); int val = 10; xQueueSend(queue, &val, 0); val = 20; xQueueSend(queue, &val, 0); val = 30; BaseType_t result = xQueueSend(queue, &val, 0);What is the value of
result after the last send call?Solution
Step 1: Analyze queue creation and sends
The queue can hold 2 integers. Two sends with zero wait fill it completely.Step 2: Evaluate last send with zero wait
The third send tries to add an item when the queue is full and does not wait, so it fails returning errQUEUE_FULL.Final Answer:
errQUEUE_FULL (send failed due to full queue) -> Option BQuick Check:
Queue full + zero wait = send fails with errQUEUE_FULL [OK]
- Assuming last send blocks
- Thinking last send overwrites data
- Confusing pdPASS and pdFAIL
xQueueReceive never return pdPASS?
QueueHandle_t queue = xQueueCreate(1, sizeof(int)); int data; // Task A xQueueReceive(queue, &data, 0); // Task B int val = 5; xQueueSend(queue, &val, 0);
Solution
Step 1: Understand queue state at receive
Task A calls receive immediately with zero wait, but queue is empty because Task B hasn't sent yet.Step 2: Effect of zero wait on empty queue
With zero wait, receive returns immediately with failure since no data is available yet.Final Answer:
Task A tries to receive before Task B sends, queue is empty, no wait time. -> Option DQuick Check:
Receive zero wait + empty queue = no pdPASS [OK]
- Thinking queue size 1 means no data can be stored
- Assuming send fails with zero wait
- Confusing pointer usage in receive
Solution
Step 1: Understand non-blocking send requirement
The task must never block, so waiting indefinitely or for a long time is not allowed.Step 2: Choose correct send method and handling
Using xQueueSend with zero block time returns immediately. Checking the return value lets the task discard data if the queue is full.Step 3: Evaluate other options
Use xQueueSend with portMAX_DELAY to wait indefinitely until space is available. blocks indefinitely, violating requirement. Use xQueueOverwrite to replace the oldest data in the queue automatically. overwrites data but may lose important info. Increase the queue size dynamically when full. is not supported by FreeRTOS.Final Answer:
Use xQueueSend with zero block time and check the return value to discard data if full. -> Option CQuick Check:
Non-blocking send + check return = safe discard [OK]
- Using blocking send when non-blocking needed
- Assuming queue size can grow dynamically
- Ignoring return value of send
Ready to prove it?
Pass the test and this pattern moves unknown
