Peek at Queue Data Without Removing It Using xQueuePeek
Read the oldest FreeRTOS queue item without removing it using xQueuePeek, and pair it with xQueueOverwrite to share a latest-value status between tasks.

xQueueReceive takes an item out of the queue: once one task has it, no other task will ever see it. That is right for commands and work items, and wrong for a status value that several tasks want to read, such as the latest temperature or the current operating mode. For those you want to look at the data and leave it in place. FreeRTOS gives you xQueuePeek for the look, and a companion call, xQueueOverwrite, for the matching write. Together they make a safe "latest value" mailbox with no mutex. This post shows both calls, builds a status mailbox with several readers, explains the two ways peeking can surprise you and lists when to use a queue for this and when to use something else. It uses FreeRTOS Kernel V11.3.3. The cornerstone guide on FreeRTOS queues, stream buffers and message buffers puts peeking among the other tools, and the earlier post on xQueueSend and xQueueReceive covers the basics.
What xQueuePeek does
BaseType_t xQueuePeek( QueueHandle_t xQueue,
void * const pvBuffer,
TickType_t xTicksToWait );xQueuePeek has the same shape as xQueueReceive. It waits up to xTicksToWait for an item, and when one is available it copies the item at the head into pvBuffer. The difference is what happens next: the item stays in the queue, still at the head, and the queue's count does not change. A later xQueueReceive, or another xQueuePeek, sees the same item.
The return value is pdPASS if an item was copied and errQUEUE_EMPTY if the wait ended with nothing there. As with receive, the buffer must be at least as large as the item size, and a blocked peek wakes when data arrives.
Overwriting: the matching write
If the queue has a single slot, a normal xQueueSend to a full queue blocks or fails. For a status mailbox you want the opposite: the writer should replace the old value without waiting, because only the latest matters. xQueueOverwrite does exactly that, and it is defined only for queues of length one.
BaseType_t xQueueOverwrite( QueueHandle_t xQueue,
const void * pvItemToQueue );It never blocks and always returns pdPASS, because it writes whether the slot is empty or full. If the slot holds an old value, that value is replaced by the new one.
A status mailbox with three readers
Create a queue of length one and use it as a mailbox. One task, the sensor task, writes the latest reading. Three other tasks peek at it whenever they need it:
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
typedef struct {
int16_t temperature_c10; /* tenths of a degree Celsius */
uint32_t tick;
} reading_t;
static QueueHandle_t xLatest;
static void vSensorTask(void *p)
{
int16_t t = 200;
(void) p;
for (;;)
{
reading_t r = { .temperature_c10 = t++, .tick = xTaskGetTickCount() };
xQueueOverwrite(xLatest, &r);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
static void vDisplayTask(void *p)
{
reading_t r;
(void) p;
for (;;)
{
if (xQueuePeek(xLatest, &r, pdMS_TO_TICKS(1000)) == pdPASS)
{
printf("display: %d.%d C\n", r.temperature_c10 / 10, r.temperature_c10 % 10);
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
static void vLoggerTask(void *p)
{
reading_t r;
(void) p;
for (;;)
{
if (xQueuePeek(xLatest, &r, portMAX_DELAY) == pdPASS)
{
printf("log: t=%lu value=%d\n", (unsigned long) r.tick, r.temperature_c10);
}
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void mailbox_start(void)
{
xLatest = xQueueCreate(1, sizeof(reading_t));
configASSERT(xLatest != NULL);
xTaskCreate(vSensorTask, "sensor", 256, NULL, 3, NULL);
xTaskCreate(vDisplayTask, "display", 256, NULL, 2, NULL);
xTaskCreate(vLoggerTask, "logger", 256, NULL, 1, NULL);
}The sensor overwrites every 500 ms. The display peeks once a second, and the logger every two. Nobody consumes the item, so every peek returns the newest reading at the time of the call, and the three readers never interfere with each other:
display: 20.0 C
log: t=2 value=200
display: 20.2 C
display: 20.4 C
log: t=2002 value=204The exact timestamps depend on start-up timing, and the point of the sample is the shape. The first peek by each reader waits for the first write, since the queue starts empty. After that, the display and the logger read independent snapshots at their own rates, and the sensor never waits for either of them.
This pattern fixes a common bug. Sharing a global reading_t between a writer task and readers risks a torn read, where a reader sees the new temperature_c10 and the old tick, because a struct write is not atomic. The queue copies the whole item as a unit inside the kernel's critical section, so every reader gets a consistent snapshot.
Peek and receive on a normal queue
On a queue with more than one slot, xQueuePeek shows the oldest item without removing it. A common use is to look at a message's header to decide whether this task should take it:
typedef struct { uint8_t dest; uint8_t payload[15]; } frame_t;
frame_t f;
if (xQueuePeek(xFrameQueue, &f, 0) == pdPASS && f.dest == MY_ADDRESS)
{
xQueueReceive(xFrameQueue, &f, 0); /* now take it */
handle_frame(&f);
}This works only when one task consumes from the queue. With two consumers, the item you peeked at can be taken by the other task between your peek and your receive, and your receive then removes a different item, or finds the queue empty. If you must inspect and then take with several consumers, give each its own queue and let a dispatcher route items.
What to watch for
A blocked peek does not consume. When a peek waits and data arrives, the peeking task wakes but the item remains, so if a receiver also waits, it is woken as well. The kernel documentation notes that a task can block on peek and be unblocked together with receivers. Design so that doesn't surprise you: a peeking task is a reader, not a consumer.
A stale value is still a value. With a mailbox, a peek always returns something once the first write happened, even if the writer has crashed. If staleness matters, put a timestamp in the item, as the example does with tick, and check its age:
if ((int32_t) (xTaskGetTickCount() - r.tick) > (int32_t) pdMS_TO_TICKS(2000))
{
printf("sensor data is stale\n");
}**xQueueOverwrite is for length one only.** It is documented as intended for queues of length one, and it is wrong for a queue that is meant to buffer a sequence, since it would silently destroy the oldest item.
The ISR variants exist. From an interrupt use xQueuePeekFromISR and xQueueOverwriteFromISR, which take the usual higher-priority-task-woken flag. xQueuePeekFromISR has restrictions, and it cannot block.
Testing the mailbox
A mailbox is simple enough to test with a few lines. Write a known sequence from one task and check that every reader sees values that never go backwards. A reader that sees a value older than one it saw before has found a bug in your ordering assumptions, and the check costs one comparison.
static uint32_t last_seen;
void check_monotonic(const reading_t *r)
{
if ((int32_t) (r->tick - last_seen) < 0)
{
printf("mailbox went backwards: %lu after %lu\n",
(unsigned long) r->tick, (unsigned long) last_seen);
}
last_seen = r->tick;
}Silence from this check over a long run is the evidence you want, since the kernel's queue copy is what guarantees a consistent snapshot.
Choosing the right tool
| You want | Use |
|---|---|
| Consume each message once | xQueueSend and xQueueReceive |
| Share the latest value with several readers | Length-one queue, xQueueOverwrite and xQueuePeek |
| Wake a task with no data | Task notification |
| Several fields updated together under a lock | A mutex around a shared struct |
| Wait for several conditions | Event group |
The mailbox is the lightest correct way to publish a value. A mutex-protected global works too, with more code and a lock that readers must take, which can cause priority inversion if a low-priority reader holds it.
The boundary
A mailbox holds one item. It has no history, so a reader that wakes up late has missed every intermediate value. If each reading matters, use a normal queue with a single consumer, or one queue per consumer. Peeking is also copy-based like every queue operation, so large items cost time on each peek, a post on queue copying explains why.


