Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Queue set for monitoring multiple queues in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Queue set for monitoring multiple queues
Create multiple queues
↓
Create a queue set
↓
Add queues to queue set
↓
Wait on queue set for data
↓
Queue set signals which queue has data
↓
Read data from the signaled queue
↓
Process data and repeat
This flow shows how multiple queues are grouped into a queue set, allowing a task to wait on all queues at once and react to whichever queue receives data first.
Execution Sample
FreeRTOS
QueueHandle_t queue1 = xQueueCreate(5, sizeof(int));
QueueHandle_t queue2 = xQueueCreate(5, sizeof(int));
QueueSetHandle_t queueSet = xQueueCreateSet(10);
xQueueAddToSet(queue1, queueSet);
xQueueAddToSet(queue2, queueSet);
QueueSetMemberHandle_t activatedQueue = xQueueSelectFromSet(queueSet, portMAX_DELAY);
This code creates two queues, adds them to a queue set, and waits indefinitely for data on any queue in the set.
Execution Table
StepActionQueue Set StateActivated QueueOutput/Result
1Create queue1EmptyNonequeue1 created with length 5
2Create queue2EmptyNonequeue2 created with length 5
3Create queueSet with length 10EmptyNonequeueSet created
4Add queue1 to queueSetqueueSet contains queue1Nonequeue1 added to queueSet
5Add queue2 to queueSetqueueSet contains queue1, queue2Nonequeue2 added to queueSet
6Task waits on queueSetqueueSet contains queue1, queue2NoneWaiting for data on any queue
7Data sent to queue2queueSet contains queue1, queue2queue2queueSet signals queue2
8Task reads data from queue2queueSet contains queue1, queue2queue2Data received from queue2
9Task waits again on queueSetqueueSet contains queue1, queue2NoneWaiting for data on any queue
10Data sent to queue1queueSet contains queue1, queue2queue1queueSet signals queue1
11Task reads data from queue1queueSet contains queue1, queue2queue1Data received from queue1
12No more data, task waitsqueueSet contains queue1, queue2NoneWaiting indefinitely
13Timeout or stop conditionqueueSet contains queue1, queue2NoneExit wait or timeout
14Cleanup queues and queueSetEmptyNoneQueues and queueSet deleted
💡 Task stops waiting due to timeout or external stop signal
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 7After Step 10Final
queue1Created emptyIn queueSetIn queueSetIn queueSetIn queueSetDeleted
queue2Created emptyNot in queueSetIn queueSetIn queueSet (activated)In queueSetDeleted
queueSetEmptyContains queue1Contains queue1, queue2Contains bothContains bothDeleted
activatedQueueNoneNoneNonequeue2queue1None
Key Moments - 3 Insights
Why does the task wait on the queue set instead of individual queues?
Waiting on the queue set allows the task to block on multiple queues at once, waking only when any queue has data, as shown in execution_table rows 6, 9, and 12.
What happens if multiple queues have data at the same time?
The queue set signals one queue at a time. The task reads from the activated queue before waiting again, as seen in rows 7-8 and 10-11.
Can queues be removed from the queue set after adding?
Yes, FreeRTOS provides xQueueRemoveFromSet() to remove queues from a queue set.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the queue set first signal a queue with data?
AStep 7
BStep 6
CStep 9
DStep 10
💡 Hint
Check the 'Activated Queue' column to see when it changes from None to a queue.
According to the variable tracker, what is the state of 'activatedQueue' after step 10?
Aqueue2
BNone
Cqueue1
DqueueSet
💡 Hint
Look at the 'activatedQueue' row under 'After Step 10' column.
If data was sent to both queues before the task waits again, how would the execution table change?
AThe queue set would signal both queues simultaneously.
BThe queue set would signal one queue first, then the other in subsequent waits.
CThe task would read from both queues in the same step.
DThe task would ignore the second queue's data.
💡 Hint
Refer to key_moments about how queue set signals queues one at a time.
Concept Snapshot
Queue sets group multiple queues to let a task wait on all at once.
Create queues, create a queue set, add queues to it.
Use xQueueSelectFromSet() to wait for any queue with data.
Task reads from the activated queue, then waits again.
Queues can be removed using xQueueRemoveFromSet(queue, queueSet).
Useful to simplify waiting on multiple queues efficiently.
Full Transcript
In FreeRTOS, a queue set lets a task monitor multiple queues simultaneously. First, create individual queues. Then create a queue set with enough length to hold all queues combined. Add each queue to the queue set. The task then waits on the queue set using xQueueSelectFromSet(), which blocks until any queue in the set has data. When data arrives on a queue, the queue set signals which queue is ready. The task reads from that queue, processes the data, and waits again. This avoids polling each queue separately and simplifies synchronization. The execution table shows each step from queue creation, adding to the set, waiting, data arrival, reading, and cleanup. The variable tracker follows the state of queues and the activated queue handle. Key moments clarify why waiting on the set is efficient, how multiple queues with data are handled one by one, and how queues can be removed from the set using xQueueRemoveFromSet(). The visual quiz tests understanding of when queues signal, variable states, and behavior with multiple data arrivals. This method helps manage multiple data sources in embedded systems cleanly and efficiently.

Practice

(1/5)
1. What is the main purpose of a Queue Set in FreeRTOS?
easy
A. To monitor multiple queues with a single wait call
B. To increase the size of a single queue
C. To create a queue that stores different data types
D. To prioritize tasks based on queue length

Solution

  1. Step 1: Recall the purpose of a Queue Set

    A Queue Set allows monitoring multiple queues or semaphores with one blocking call.
  2. 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.
  3. Final Answer:

    To monitor multiple queues with a single wait call -> Option A
  4. Quick Check:

    Queue Set = single wait for many queues [OK]
Hint: Queue Set = watch many queues with one wait [OK]
Common Mistakes:
  • Thinking Queue Set increases queue size
  • Confusing Queue Set with task priority
  • Believing Queue Set stores mixed data types
2. Which of the following is the correct way to create a queue set for monitoring two queues in FreeRTOS?
easy
A. xQueueSetCreate( queue1Length + queue2Length, itemSize )
B. xQueueSetCreate( queue1Length * queue2Length, itemSize )
C. xQueueSetCreate( queue1Length + queue2Length, sizeof(void*) )
D. xQueueSetCreate( queue1Length + queue2Length, 0 )

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    xQueueSetCreate( queue1Length + queue2Length, sizeof(void*) ) -> Option C
  4. Quick Check:

    Queue set size = sum lengths, item size = pointer size [OK]
Hint: Queue set size = sum of queues, item size = pointer size [OK]
Common Mistakes:
  • Using item size instead of pointer size
  • Multiplying queue lengths instead of adding
  • Setting item size to zero
3. Given the code snippet:
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?
medium
A. A pointer to the data item received
B. A handle to the queue that has data available
C. The total number of items in all queues
D. NULL if no queues have data

Solution

  1. Step 1: Analyze xQueueSelectFromSet behavior

    This function blocks until any queue in the set has data, then returns a handle to that queue.
  2. Step 2: Determine what activatedQueue holds

    It holds the queue handle that has data ready to be read, not the data itself or count.
  3. Final Answer:

    A handle to the queue that has data available -> Option B
  4. Quick Check:

    xQueueSelectFromSet returns queue handle with data [OK]
Hint: xQueueSelectFromSet returns queue handle with data ready [OK]
Common Mistakes:
  • Thinking it returns data pointer
  • Confusing with total item count
  • Expecting NULL when data is available
4. What is wrong with this code snippet for using a queue set?
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);
medium
A. xQueueCreate uses wrong item size
B. Queue set size is too small for the queue length
C. xQueueAddToSet is missing for q1
D. xQueueReceive is called with a queue set handle instead of a queue handle

Solution

  1. Step 1: Examine the xQueueReceive call

    The code calls xQueueReceive(set, &value, 0), passing the queue set handle 'set'.
  2. Step 2: Recall the return value of xQueueSelectFromSet

    It returns 'member', a handle to the queue with data available.
  3. Step 3: Identify the mistake

    xQueueReceive must use the queue handle 'member', not the queue set handle 'set'.
  4. Final Answer:

    xQueueReceive is called with a queue set handle instead of a queue handle -> Option D
  5. Quick Check:

    xQueueReceive uses queue handle from set, not set itself [OK]
Hint: xQueueSelectFromSet returns queue handle for xQueueReceive [OK]
Common Mistakes:
  • Confusing queue set handle with queue handle
  • Assuming queue set size must match queue length
  • Missing xQueueAddToSet call
5. You want to monitor three queues with lengths 4, 5, and 6 using a queue set. Which of the following is the best practice to create and use the queue set correctly?
hard
A. Create queue set with size 15, add all queues, then call xQueueSelectFromSet to wait
B. Create queue set with size 6, add all queues, then call xQueueSelectFromSet to wait
C. Create queue set with size 4, add only the largest queue, then call xQueueSelectFromSet
D. Create queue set with size 15, add queues after calling xQueueSelectFromSet

Solution

  1. 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.
  2. Step 2: Add all queues before waiting

    All queues must be added to the set before calling xQueueSelectFromSet to monitor them properly.
  3. 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.
  4. Final Answer:

    Create queue set with size 15, add all queues, then call xQueueSelectFromSet to wait -> Option A
  5. Quick Check:

    Queue set size = sum lengths, add all queues first [OK]
Hint: Queue set size = sum of queues; add all before waiting [OK]
Common Mistakes:
  • Using queue set size smaller than total queue lengths
  • Adding queues after waiting
  • Adding only some queues to the set
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard