A queue set lets you watch many queues at once. You can see which queue has data ready without checking each one separately.
Queue set for monitoring multiple queues in FreeRTOS
/* Create a queue set */ QueueSetHandle_t xQueueSet = xQueueCreateSet( totalNumberOfItems ); /* Add queues to the set */ xQueueAddToSet( xQueueHandle, xQueueSet ); /* Wait for any queue in the set to have data */ QueueHandle_t xActivatedQueue = xQueueSelectFromSet( xQueueSet, xTicksToWait );
xQueueCreateSet needs the total number of items all queues can hold combined.
xQueueSelectFromSet blocks until any queue in the set has data or timeout expires.
/* Create a queue set for 10 items total */ QueueSetHandle_t xQueueSet = xQueueCreateSet( 10 ); /* Add two queues to the set */ xQueueAddToSet( xQueue1, xQueueSet ); xQueueAddToSet( xQueue2, xQueueSet );
/* Wait forever for any queue in the set to have data */
QueueHandle_t xActivatedQueue = xQueueSelectFromSet( xQueueSet, portMAX_DELAY );/* Handle empty queue set case */ if (xQueueSet == NULL) { // Handle error: queue set not created }
This program creates two queues and a queue set to monitor them. It sends data to both queues, waits for any queue to have data, then prints which queue received data and the value.
#include "FreeRTOS.h" #include "queue.h" #include "task.h" #include <stdio.h> /* Create two queues and a queue set to monitor both */ void vQueueSetExample(void *pvParameters) { QueueHandle_t xQueue1 = xQueueCreate( 5, sizeof(int) ); QueueHandle_t xQueue2 = xQueueCreate( 5, sizeof(int) ); if (xQueue1 == NULL || xQueue2 == NULL) { printf("Failed to create queues\n"); vTaskDelete(NULL); } QueueSetHandle_t xQueueSet = xQueueCreateSet( 10 ); if (xQueueSet == NULL) { printf("Failed to create queue set\n"); vTaskDelete(NULL); } xQueueAddToSet( xQueue1, xQueueSet ); xQueueAddToSet( xQueue2, xQueueSet ); int value1 = 100; int value2 = 200; /* Send data to queues */ xQueueSend( xQueue1, &value1, 0 ); xQueueSend( xQueue2, &value2, 0 ); printf("Waiting for data on any queue in the set...\n"); QueueHandle_t xActivatedQueue = xQueueSelectFromSet( xQueueSet, portMAX_DELAY ); if (xActivatedQueue == xQueue1) { int receivedValue; xQueueReceive( xQueue1, &receivedValue, 0 ); printf("Data received from Queue 1: %d\n", receivedValue); } else if (xActivatedQueue == xQueue2) { int receivedValue; xQueueReceive( xQueue2, &receivedValue, 0 ); printf("Data received from Queue 2: %d\n", receivedValue); } else { printf("No queue activated\n"); } vTaskDelete(NULL); } int main(void) { xTaskCreate( vQueueSetExample, "QueueSetTask", configMINIMAL_STACK_SIZE, NULL, 1, NULL ); vTaskStartScheduler(); return 0; }
The time complexity of xQueueSelectFromSet is efficient because it blocks until data is available, avoiding busy waiting.
Space complexity depends on the total size of all queues combined in the set.
Common mistake: forgetting to add queues to the queue set before waiting on it.
Use queue sets when you want to wait on multiple queues simultaneously instead of checking each queue separately.
Queue sets let you watch many queues with one wait call.
They help write simpler and more efficient code when handling multiple queues.
Always create the queue set with enough total size and add all queues before waiting.
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
