0
0
FreeRTOSprogramming~10 mins

Producer-consumer pattern in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Producer-consumer pattern
Producer Task
Produce Data
Send to Queue
Consumer Task
Receive from Queue
Consume Data
Repeat
The producer creates data and sends it to a queue. The consumer waits for data from the queue and processes it.
Execution Sample
FreeRTOS
void producer(void *pvParameters) {
  int count = 0;
  while(1) {
    xQueueSend(queue, &count, portMAX_DELAY);
    count++;
  }
}

void consumer(void *pvParameters) {
  int received;
  while(1) {
    xQueueReceive(queue, &received, portMAX_DELAY);
    // process received
  }
}
Producer sends incrementing numbers to a queue; consumer receives and processes them.
Execution Table
StepTaskActionQueue StateData Sent/ReceivedOutput
1ProducerProduce 0[]0
2ProducerSend 0 to queue[0]0
3ConsumerReceive from queue[]0Process 0
4ProducerProduce 1[]1
5ProducerSend 1 to queue[1]1
6ConsumerReceive from queue[]1Process 1
7ProducerProduce 2[]2
8ProducerSend 2 to queue[2]2
9ConsumerReceive from queue[]2Process 2
10ProducerProduce 3[]3
11ProducerSend 3 to queue[3]3
12ConsumerReceive from queue[]3Process 3
13ExitStop after 4 cycles[]End of trace
💡 Stopped after 4 produce-consume cycles for demonstration
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
count (Producer)012344
received (Consumer)N/A01233
queueempty[0]empty[1]emptyempty
Key Moments - 3 Insights
Why does the consumer wait when the queue is empty?
Because xQueueReceive uses portMAX_DELAY, the consumer blocks until data arrives, as shown in execution_table rows 3, 6, 9, and 12.
What happens if the producer tries to send when the queue is full?
In this example, the queue size is large enough, but normally xQueueSend would block or fail; here, the producer always succeeds because the consumer removes items promptly (see queue state in execution_table).
Why does the queue alternate between empty and having one item?
Because the producer sends one item, then the consumer receives it before the next send, so the queue never holds more than one item at a time (see queue state column).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the queue state after the producer sends data?
A[1]
B[]
C[0,1]
D[0]
💡 Hint
Check the 'Queue State' column at step 5 in the execution_table.
At which step does the consumer first receive data?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Task' and 'Action' columns to find when consumer receives data.
If the producer increments count by 2 instead of 1, how would the 'Data Sent/Received' column change at step 8?
AIt would show 3
BIt would show 2
CIt would show 4
DIt would show 1
💡 Hint
Check how count increments affect data sent at each produce step in variable_tracker.
Concept Snapshot
Producer-consumer pattern in FreeRTOS:
- Producer task creates data and sends it to a queue.
- Consumer task waits and receives data from the queue.
- Use xQueueSend and xQueueReceive for communication.
- Consumer blocks if queue empty; producer blocks if queue full.
- Ensures safe data exchange between tasks.
Full Transcript
This visual execution shows the producer-consumer pattern in FreeRTOS. The producer task creates data (an incrementing count) and sends it to a queue using xQueueSend. The consumer task waits for data using xQueueReceive and processes it. The queue acts as a safe buffer between tasks. The execution table traces each step: producing data, sending to queue, receiving from queue, and processing. Variables like count and received change as data flows. Key moments clarify why the consumer waits when the queue is empty and how the queue state changes. The visual quiz tests understanding of queue states and data flow. This pattern helps tasks communicate safely and efficiently in embedded systems.