xQueueSend() and xQueueReceive() in FreeRTOS - Time & Space Complexity
When working with FreeRTOS queues, it's important to know how the time to send or receive items grows as the queue size changes.
We want to understand how the cost of adding or removing items from a queue changes when the queue gets bigger.
Analyze the time complexity of the following code snippet.
// Create a queue to hold 10 integers
QueueHandle_t queue = xQueueCreate(10, sizeof(int));
int value = 42;
// Send an item to the queue
xQueueSend(queue, &value, portMAX_DELAY);
int receivedValue;
// Receive an item from the queue
xQueueReceive(queue, &receivedValue, portMAX_DELAY);
This code creates a queue, sends one integer to it, and then receives one integer from it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding or removing an item from the queue buffer.
- How many times: Each call to
xQueueSend()orxQueueReceive()handles one item at a time.
Each send or receive operation moves one item in or out of the queue without scanning the whole queue.
| Queue Size (n) | Approx. Operations per Send/Receive |
|---|---|
| 10 | Constant steps to place or take one item |
| 100 | Same constant steps, no extra work |
| 1000 | Still constant steps, independent of size |
Pattern observation: The time to send or receive does not grow with queue size; it stays about the same.
Time Complexity: O(1)
This means sending or receiving one item takes the same amount of time no matter how big the queue is.
[X] Wrong: "Sending or receiving items takes longer as the queue gets bigger because it has to check all items."
[OK] Correct: The queue uses a circular buffer and pointers, so it directly places or takes one item without scanning others.
Understanding how queue operations scale helps you explain efficient communication between tasks in embedded systems, a key skill in real-time programming.
"What if the queue was implemented as a linked list instead of a circular buffer? How would the time complexity of sending and receiving change?"
Practice
xQueueSend() function in FreeRTOS?Solution
Step 1: Understand the role of
xQueueSend()xQueueSend()is used to place data into a queue so that other tasks can retrieve it later.Step 2: Differentiate from other queue functions
Functions likexQueueReceive()remove data, whilexQueueCreate()andxQueueDelete()manage queue lifecycle, not data transfer.Final Answer:
To add an item to a queue for other tasks to receive -> Option BQuick Check:
xQueueSend()adds data [OK]
- Confusing sending with receiving
- Thinking it creates or deletes queues
- Ignoring the function's data transfer role
xQueueSend()?Solution
Step 1: Check parameter types for
The function expects the queue handle, a pointer to the data, and a timeout value.xQueueSend()Step 2: Match the correct syntax
xQueueSend(queueHandle, &data, portMAX_DELAY); correctly passes the queue handle, address of data, and a timeout constantportMAX_DELAY.Final Answer:
xQueueSend(queueHandle, &data, portMAX_DELAY); -> Option CQuick Check:
Queue handle + &data + timeout = correct syntax [OK]
- Passing data by value instead of pointer
- Omitting the timeout parameter
- Passing queue handle by address
int value = 10;
if (xQueueSend(queue, &value, 0) == pdPASS) {
int receivedValue = 0;
if (xQueueReceive(queue, &receivedValue, 0) == pdPASS) {
printf("%d", receivedValue);
}
}What will be printed when this code runs?
Solution
Step 1: Analyze
The value 10 is sent to the queue with zero wait time. If the queue is not full, it returnsxQueueSend()callpdPASS.Step 2: Analyze
The code tries to receive from the queue immediately. Since the value was just sent, it will receive 10 and print it.xQueueReceive()callFinal Answer:
10 -> Option AQuick Check:
Send 10, receive 10, print 10 [OK]
- Assuming queue is empty at receive
- Confusing return values with data
- Ignoring zero timeout behavior
int data = 5;
if (xQueueSend(queue, data, 10) != pdPASS) {
// handle error
}Solution
Step 1: Check parameter types in
The second parameter must be a pointer to the data, but here data is passed by value.xQueueSend()Step 2: Identify the error cause
Passing data by value causes a type mismatch and likely a compilation error or incorrect behavior.Final Answer:
Data should be passed by pointer, not by value -> Option AQuick Check:
Pass &data, not data [OK]
- Passing data directly instead of pointer
- Misunderstanding timeout parameter
- Ignoring return value check
xQueueSend() and xQueueReceive() to achieve this without losing data?Solution
Step 1: Understand queue data handling
Queues handle one item at a time, so each integer must be sent individually withxQueueSend().Step 2: Properly receive data
Each item must be received individually withxQueueReceive(), checking return values to avoid errors or data loss.Step 3: Eliminate incorrect options
Send all integers at once by passing an array pointer toxQueueSend(), then receive with onexQueueReceive()is wrong because queues do not send arrays as single items. Send integers without checking return values, then receive once expecting all data risks data loss by ignoring return values. UsexQueueReceive()to send data andxQueueSend()to receive data reverses function roles.Final Answer:
Send each integer withxQueueSend()using a timeout, then receive each withxQueueReceive()using a timeout, checking return values each time -> Option DQuick Check:
Send and receive items one by one, check returns [OK]
- Trying to send multiple items at once
- Ignoring return values causing lost data
- Mixing up send and receive functions
Ready to prove it?
Pass the test and this pattern moves unknown
