Queues help tasks talk to each other safely and in order. They make sure data moves between tasks without getting lost or mixed up.
Why queues are needed for inter-task communication in FreeRTOS
/* Queue handle declaration */ QueueHandle_t xQueue; /* Create a queue to hold 10 integers */ xQueue = xQueueCreate(10, sizeof(int)); /* Send data to the queue */ int dataToSend = 100; xQueueSend(xQueue, &dataToSend, portMAX_DELAY); /* Receive data from the queue */ int receivedData; xQueueReceive(xQueue, &receivedData, portMAX_DELAY);
Queues hold copies of data, so tasks do not share pointers directly.
Use portMAX_DELAY to wait indefinitely for send or receive.
/* Example: Empty queue */ QueueHandle_t xQueue = xQueueCreate(5, sizeof(int)); int receivedValue; // Trying to receive from empty queue will block or fail if(xQueueReceive(xQueue, &receivedValue, 0) == pdPASS) { // Got data } else { // No data available }
/* Example: Queue with one element */ QueueHandle_t xQueue = xQueueCreate(5, sizeof(int)); int dataToSend = 42; xQueueSend(xQueue, &dataToSend, 0); int receivedValue; xQueueReceive(xQueue, &receivedValue, 0); // Receives 42
/* Example: Queue full scenario */ QueueHandle_t xQueue = xQueueCreate(2, sizeof(int)); int data1 = 1, data2 = 2, data3 = 3; xQueueSend(xQueue, &data1, 0); xQueueSend(xQueue, &data2, 0); // Queue is full now BaseType_t result = xQueueSend(xQueue, &data3, 0); // Fails because queue is full
This program creates a queue and two tasks. The sender task puts a number into the queue. The receiver task waits and gets the number from the queue. It prints messages before and after to show the data flow.
#include "FreeRTOS.h" #include "task.h" #include "queue.h" #include <stdio.h> QueueHandle_t xQueue; void vSenderTask(void *pvParameters) { int dataToSend = 10; printf("Sender: Sending %d to queue\n", dataToSend); xQueueSend(xQueue, &dataToSend, portMAX_DELAY); vTaskDelete(NULL); } void vReceiverTask(void *pvParameters) { int receivedData = 0; if(xQueueReceive(xQueue, &receivedData, portMAX_DELAY) == pdPASS) { printf("Receiver: Received %d from queue\n", receivedData); } else { printf("Receiver: Failed to receive data\n"); } vTaskDelete(NULL); } int main(void) { xQueue = xQueueCreate(5, sizeof(int)); if(xQueue == NULL) { printf("Failed to create queue\n"); return 1; } xTaskCreate(vSenderTask, "Sender", 1000, NULL, 1, NULL); xTaskCreate(vReceiverTask, "Receiver", 1000, NULL, 1, NULL); vTaskStartScheduler(); // Should never reach here return 0; }
Time complexity of sending or receiving from a queue is generally O(1).
Queues use extra memory to store copies of data, so consider space when choosing queue size.
Common mistake: Trying to share pointers instead of data copies can cause errors.
Use queues when you want safe, ordered communication between tasks. Use other methods like direct task notifications for simpler signals.
Queues let tasks send and receive data safely without conflicts.
They keep data in order and handle different task speeds.
Queues are essential for clear and reliable communication between tasks in FreeRTOS.
Practice
Solution
Step 1: Understand the role of queues in task communication
Queues allow tasks to exchange data safely without overwriting or losing information.Step 2: Identify the main purpose of queues in FreeRTOS
They prevent conflicts by managing data flow between tasks running at different speeds.Final Answer:
To safely send and receive data between tasks without conflicts -> Option DQuick Check:
Queues ensure safe data exchange = A [OK]
- Thinking queues speed up single tasks
- Confusing queues with permanent storage
- Believing queues duplicate tasks
Solution
Step 1: Recall the FreeRTOS queue creation function
The correct function to create a queue isxQueueCreate.Step 2: Verify the syntax of the function call
The function takes two parameters: the number of items and the size of each item.Final Answer:
xQueueCreate(10, sizeof(int)); -> Option AQuick Check:
Queue creation uses xQueueCreate() = A [OK]
- Using incorrect function names like xQueueStart
- Mixing up function name capitalization
- Omitting parameters or using wrong ones
QueueHandle_t queue = xQueueCreate(2, sizeof(int));
int value = 5;
xQueueSend(queue, &value, 0);
value = 10;
xQueueSend(queue, &value, 0);
int received;
xQueueReceive(queue, &received, 0);
printf("%d", received);Solution
Step 1: Understand queue send order
The first value sent is 5, then 10. Queues keep order FIFO (first in, first out).Step 2: Check which value is received first
The first received value is 5, the first sent value.Final Answer:
5 -> Option BQuick Check:
Queue FIFO order means first sent is first received = C [OK]
- Assuming last sent value is received first
- Confusing queue with stack behavior
- Expecting zero instead of actual data
QueueHandle_t queue = xQueueCreate(1, sizeof(int)); int value = 42; xQueueSend(queue, &value, 0); xQueueSend(queue, &value, 0);
Solution
Step 1: Analyze queue size and send operations
The queue can hold only 1 item, but two sends are done without receiving.Step 2: Identify the problem caused by queue overflow
The second send will fail or block because the queue is full.Final Answer:
The queue size is too small for two sends without receiving -> Option CQuick Check:
Queue size limits sends = D [OK]
- Thinking int is invalid data type for queues
- Misunderstanding xQueueSend parameters
- Believing queue size can be zero
Solution
Step 1: Understand task speed differences
Task A produces data irregularly, Task B processes it at its own pace.Step 2: Identify queue benefits for this scenario
Queues buffer data safely and keep order, allowing tasks to work independently without data loss.Step 3: Eliminate incorrect options
Queues do not force speed matching, do not delete data automatically, and do not share variables directly.Final Answer:
Because queues buffer data safely and handle different task speeds -> Option AQuick Check:
Queues buffer and sync tasks = B [OK]
- Thinking queues force tasks to run at same speed
- Believing queues delete old data automatically
- Confusing queues with shared variable access
Ready to prove it?
Pass the test and this pattern moves unknown
