Queue set for monitoring multiple queues in FreeRTOS - Time & Space Complexity
When using a queue set in FreeRTOS, we want to know how checking multiple queues affects the time it takes to find data.
We ask: How does the time to check queues grow as we add more queues to the set?
Analyze the time complexity of the following FreeRTOS code snippet.
QueueSetHandle_t xQueueSet = xQueueCreateSet( 3 * queue_LENGTH );
xQueueAddToSet( xQueue1, xQueueSet );
xQueueAddToSet( xQueue2, xQueueSet );
xQueueAddToSet( xQueue3, xQueueSet );
QueueSetMemberHandle_t xActivatedMember = xQueueSelectFromSet( xQueueSet, portMAX_DELAY );
if( xActivatedMember == xQueue1 ) {
// Process xQueue1
} else if( xActivatedMember == xQueue2 ) {
// Process xQueue2
} else if( xActivatedMember == xQueue3 ) {
// Process xQueue3
}
This code creates a queue set from three queues and waits to see which queue has data ready to process.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Receive from the queue set's internal event queue (no loop over queues).
- How many times: Constant time (O(1)) per call to
xQueueSelectFromSet.
As the number of queues in the set grows, the time to check which queue is ready remains constant (O(1)).
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 1 operation |
| 10 | 1 operation |
| 100 | 1 operation |
Pattern observation: The number of operations stays constant regardless of the number of queues.
Time Complexity: O(1)
This means the time to find which queue has data is constant, independent of the number of queues in the set.
[X] Wrong: "Checking multiple queues in a queue set requires scanning all queues linearly."
[OK] Correct: FreeRTOS queue sets use an internal event queue that stores handles of ready queues, enabling O(1) selection without scanning.
Understanding how queue sets scale helps you design responsive systems that handle multiple data sources efficiently.
"What if the queue set used a priority system to check queues? How would that affect the time complexity?"
Practice
Queue Set in FreeRTOS?Solution
Step 1: Recall the purpose of a Queue Set
A Queue Set allows monitoring multiple queues or semaphores with one blocking call.Step 2: Match options to the Queue Set purpose
Only "To monitor multiple queues with a single wait call" correctly describes this purpose; others describe unrelated features.Final Answer:
To monitor multiple queues with a single wait call -> Option AQuick Check:
Queue Set = single wait for many queues [OK]
- Thinking Queue Set increases queue size
- Confusing Queue Set with task priority
- Believing Queue Set stores mixed data types
Solution
Step 1: Review parameters of xQueueSetCreate
The first parameter is the total number of items the set can hold, usually sum of queue lengths. The second is the size of a pointer (sizeof(void*)) because queue sets hold references to queues.Step 2: Evaluate each option
xQueueSetCreate( queue1Length + queue2Length, sizeof(void*) ) correctly uses sum of lengths and pointer size. Others are incorrect: itemSize, multiplication, or zero size.Final Answer:
xQueueSetCreate( queue1Length + queue2Length, sizeof(void*) ) -> Option CQuick Check:
Queue set size = sum lengths, item size = pointer size [OK]
- Using item size instead of pointer size
- Multiplying queue lengths instead of adding
- Setting item size to zero
QueueHandle_t queue1 = xQueueCreate(5, sizeof(int)); QueueHandle_t queue2 = xQueueCreate(3, sizeof(int)); QueueSetHandle_t queueSet = xQueueSetCreate(8, sizeof(void*)); xQueueAddToSet(queue1, queueSet); xQueueAddToSet(queue2, queueSet); QueueSetMemberHandle_t activatedQueue = xQueueSelectFromSet(queueSet, portMAX_DELAY);
What does
activatedQueue represent after xQueueSelectFromSet returns?Solution
Step 1: Analyze xQueueSelectFromSet behavior
This function blocks until any queue in the set has data, then returns a handle to that queue.Step 2: Determine what activatedQueue holds
It holds the queue handle that has data ready to be read, not the data itself or count.Final Answer:
A handle to the queue that has data available -> Option BQuick Check:
xQueueSelectFromSet returns queue handle with data [OK]
- Thinking it returns data pointer
- Confusing with total item count
- Expecting NULL when data is available
QueueSetHandle_t set = xQueueSetCreate(5, sizeof(void*)); QueueHandle_t q1 = xQueueCreate(3, sizeof(int)); xQueueAddToSet(q1, set); QueueSetMemberHandle_t member = xQueueSelectFromSet(set, portMAX_DELAY); int value; xQueueReceive(set, &value, 0);
Solution
Step 1: Examine the xQueueReceive call
The code calls xQueueReceive(set, &value, 0), passing the queue set handle 'set'.Step 2: Recall the return value of xQueueSelectFromSet
It returns 'member', a handle to the queue with data available.Step 3: Identify the mistake
xQueueReceive must use the queue handle 'member', not the queue set handle 'set'.Final Answer:
xQueueReceive is called with a queue set handle instead of a queue handle -> Option DQuick Check:
xQueueReceive uses queue handle from set, not set itself [OK]
- Confusing queue set handle with queue handle
- Assuming queue set size must match queue length
- Missing xQueueAddToSet call
Solution
Step 1: Calculate total queue set size
The queue set size must be at least the sum of all queue lengths: 4 + 5 + 6 = 15.Step 2: Add all queues before waiting
All queues must be added to the set before calling xQueueSelectFromSet to monitor them properly.Step 3: Evaluate options
Create queue set with size 15, add all queues, then call xQueueSelectFromSet to wait follows best practice: correct size, all queues added, then wait. Others fail on size, timing, or completeness.Final Answer:
Create queue set with size 15, add all queues, then call xQueueSelectFromSet to wait -> Option AQuick Check:
Queue set size = sum lengths, add all queues first [OK]
- Using queue set size smaller than total queue lengths
- Adding queues after waiting
- Adding only some queues to the set
Ready to prove it?
Pass the test and this pattern moves unknown
