Send Data Between Tasks With xQueueSend and xQueueReceive
Pass messages between FreeRTOS tasks with a queue: create it, send with xQueueSend, receive with xQueueReceive, set timeouts and check the return values.

Two tasks that need to share data have a choice: share a variable and protect it, or pass the data through a channel the kernel manages. A FreeRTOS queue is that channel. One task sends an item, another receives it, and the kernel takes care of waiting, waking and ordering, so neither task needs a lock. A queue is the right default for task-to-task communication, and xQueueSend and xQueueReceive are the two functions you will write most. This post builds a complete producer and consumer, explains each parameter in plain terms, shows what happens when the queue is empty or full and lists the mistakes beginners make. It uses FreeRTOS Kernel V11.3.3. The cornerstone guide on FreeRTOS queues, stream buffers and message buffers covers the whole family, and the earlier post on creating your first tasks provides the task basics this one assumes.
What a queue is
A queue is a first-in, first-out buffer with a fixed capacity and a fixed item size, both chosen when you create it. Tasks add items at the back and remove items from the front. If a task tries to receive from an empty queue, it can wait, blocked and using no CPU, until an item arrives. If a task tries to send to a full queue, it can wait until there is room.
You create a queue with xQueueCreate, giving the number of items and the size of one item in bytes:
#include "FreeRTOS.h"
#include "queue.h"
typedef struct {
uint8_t channel;
uint16_t value;
} adc_sample_t;
static QueueHandle_t xSampleQueue;
void queue_init(void)
{
xSampleQueue = xQueueCreate(8, sizeof(adc_sample_t));
configASSERT(xSampleQueue != NULL);
}The call returns a handle, or NULL if the heap could not supply the storage. The storage needed is the item size times the length, plus a small control structure, all allocated at creation. Here, eight samples at four bytes each, because the struct pads to four bytes.
Sending
BaseType_t xQueueSend( QueueHandle_t xQueue,
const void * pvItemToQueue,
TickType_t xTicksToWait );xQueueSend copies the item the pointer refers to into the queue. It returns pdPASS if the item was queued and errQUEUE_FULL if the queue stayed full for the whole wait. The third parameter is how long to wait for space, in ticks: 0 returns at once if the queue is full, a number of ticks waits up to that long and portMAX_DELAY waits indefinitely, which requires INCLUDE_vTaskSuspend to be 1.
xQueueSend is a macro for xQueueSendToBack. There is also xQueueSendToFront, which puts an item at the head so it is received first, useful for urgent messages.
Receiving
BaseType_t xQueueReceive( QueueHandle_t xQueue,
void * pvBuffer,
TickType_t xTicksToWait );xQueueReceive copies the oldest item into the buffer you provide, and removes it from the queue. It returns pdPASS if an item arrived and errQUEUE_EMPTY if the wait expired with nothing there. The buffer must be at least as big as the item size given at creation, since the kernel copies exactly that many bytes.
A complete producer and consumer
The producer takes samples and sends them. The consumer waits for samples and processes them. Both tasks run in a loop, and neither calls the other:
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
static void vProducer(void *pvParameters)
{
uint16_t counter = 0;
(void) pvParameters;
for (;;)
{
adc_sample_t s = { .channel = 1, .value = counter++ };
if (xQueueSend(xSampleQueue, &s, pdMS_TO_TICKS(10)) != pdPASS)
{
printf("producer: queue full, sample %u dropped\n", s.value);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
static void vConsumer(void *pvParameters)
{
adc_sample_t s;
(void) pvParameters;
for (;;)
{
if (xQueueReceive(xSampleQueue, &s, portMAX_DELAY) == pdPASS)
{
printf("consumer: channel %u value %u\n", s.channel, s.value);
}
}
}
int main(void)
{
board_init();
queue_init();
xTaskCreate(vProducer, "prod", 256, NULL, 1, NULL);
xTaskCreate(vConsumer, "cons", 256, NULL, 2, NULL);
vTaskStartScheduler();
for (;;) { }
}The consumer has the higher priority, and it blocks forever on the queue. When the producer sends, the consumer becomes ready, preempts the producer and prints the sample. Then it blocks again, and the producer sleeps 100 ms. The printed output is:
consumer: channel 1 value 0
consumer: channel 1 value 1
consumer: channel 1 value 2One line appears every 100 ms. The consumer used no CPU between lines, because it was blocked on an empty queue, which is the main benefit of a queue over polling a flag.
What happens when the consumer is slower
Swap the priorities, or add a long delay in the consumer, and the queue fills up. The producer's xQueueSend waits up to 10 ms for space, then returns errQUEUE_FULL, and the message about a dropped sample prints. You choose the policy: wait, drop or overwrite.
producer: queue full, sample 24 dropped
producer: queue full, sample 25 droppedThe values show the pattern. A full queue is data about your design. It means the consumer cannot keep up on average, or that the queue is too small for a burst. The sizing post covers the arithmetic for choosing the length.
Checking the state of a queue
Three functions report the state without blocking:
UBaseType_t waiting = uxQueueMessagesWaiting(xSampleQueue);
UBaseType_t spaces = uxQueueSpacesAvailable(xSampleQueue);
xQueueReset(xSampleQueue); /* empties the queue */uxQueueMessagesWaiting tells you how many items are queued. They are useful for diagnostics and for logging the high-water mark of a queue, but do not base logic on them, since the value can change the moment after you read it.
Mistakes to avoid
Passing the wrong size. The item size given to xQueueCreate must match the type you send. Creating a queue of uint8_t items and then sending a uint32_t copies only the first byte, with no warning. Use sizeof on the type, not a number.
Sending a pointer's value when you meant its target. xQueueSend(q, ptr, ...) copies what ptr points to. To send the pointer itself, pass the address of the pointer variable. The post on why queues copy data covers that choice.
Ignoring the return value. A queue that is full or empty returns an error code, and ignoring it loses data silently or processes a stale buffer. Check it.
Calling from an interrupt. xQueueSend and xQueueReceive are for tasks. In an interrupt, use xQueueSendFromISR, which the ISR post explains.
Waiting forever in a task that needs to do other things. A consumer blocked on a queue with portMAX_DELAY cannot also watch for a stop request. Use a finite timeout and check, or use a queue set.
Creating a queue after the tasks start. If a task runs before xSampleQueue is assigned, it uses NULL. Create queues before the scheduler starts, or before the tasks that use them.
Reading the wait times
The timeout parameter is the part of the API that shapes behavior most. A zero wait is right for an interrupt-adjacent path where blocking is not an option, and for a task that wants to try and move on. A short finite wait, such as 10 ms, turns a stuck consumer into an error you can log. An infinite wait is right for a task whose only job is to serve the queue. Pick the wait deliberately for each call, and write down why, because it decides what a failure looks like in the field.
The boundary
A queue copies its items, so large items cost time on every send and receive. For big buffers, send a pointer to the data and keep a clear rule about who owns it, which a later post shows. A queue also has no notion of "latest value only", so for a status that should always show the newest value, the xQueueOverwrite and xQueuePeek pair on a queue of length one is the better tool.


