Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 518 of 9367. Real-Time Operating Systems (RTOS)Free lesson

Optimizing Producer-Consumer Patterns with Queue Sets

Design efficient multi-source data collection systems by grouping multiple queues into a single set for unified monitoring and simplified task logic.

In this lesson, you will learn how to reduce system overhead and simplify task architecture by monitoring multiple data streams through a single, unified queue set interface.

The Challenge of Multiple Sources

In complex embedded systems, a single consumer task often needs to process data from various independent producers—such as a UART buffer, a sensor ADC, and a network socket. If you assign a separate task to block on each queue, you waste system memory and CPU cycles on unnecessary context switches. Conversely, if your consumer task sequentially polls each queue, it risks high latency: if the task is busy checking an empty sensor queue, it might miss an urgent packet arriving on the UART queue.

The Queue Set Solution

A Queue Set allows a consumer task to register multiple queues into a single "set" object. Instead of checking each queue individually, the task blocks on the set itself. The RTOS kernel monitors the entire group, and the consumer task is woken up immediately when any of the queues within the set receive data.

Consider this logic flow:

// Grouping multiple producers into one monitor
QueueSetHandle_t xQueueSet = xQueueCreateSet(TOTAL_CAPACITY);
xQueueAddToSet(xUartQueue, xQueueSet);
xQueueAddToSet(xSensorQueue, xQueueSet);

// Consumer task blocks once
QueueSetMemberHandle_t xActivatedMember;
while(1) {
    // Blocks until data arrives on ANY queue in the set
    xActivatedMember = xQueueSelectFromSet(xQueueSet, portMAX_DELAY);
    
    if (xActivatedMember == xUartQueue) {
        // Process UART data
    } else if (xActivatedMember == xSensorQueue) {
        // Process sensor data
    }
}

Why This is Efficient

By using a Queue Set, you eliminate the "busy-wait" polling loop and avoid the architectural bloat of creating one task per data source. The RTOS scheduler maintains the block state efficiently, ensuring that your CPU only wakes up when there is actual work to perform. This maintains the determinism required for real-time systems by reducing the time between data arrival and task execution (the reaction latency), all while keeping your task count—and thus your stack usage—at a minimum.

Key Takeaway: Queue Sets enable a single consumer to multiplex data from multiple producers by blocking on a collective group rather than polling individual sources, significantly reducing latency and memory overhead.