Updated May 2025

xQueuePeek

[Queue Management]

queue.h

1 BaseType_t xQueuePeek(
2 QueueHandle_t xQueue,
3 void *pvBuffer,
4 TickType_t xTicksToWait
5 );

This is a macro that calls the xQueueGenericReceive() function.

Receive an item from a queue without removing the item from the queue. The item is received by copy so a buffer of adequate size must be provided. The number of bytes copied into the buffer was defined when the queue was created.

Successfully received items remain on the queue so will be returned again by the next call, or a call to xQueueReceive().

This macro must not be used in an interrupt service routine.

Parameters:

  • xQueue

    The handle to the queue from which the item is to be received.

  • pvBuffer

    Pointer to the buffer into which the received item will be copied. This must be at least large enough to hold the size of the queue item defined when the queue was created.

  • xTicksToWait

    The maximum amount of time the task should block waiting for an item to receive should the queue be empty at the time of the call. The time is defined in tick periods so the constant portTICK_PERIOD_MS should be used to convert to real time if this is required.
    If INCLUDE_vTaskSuspend is set to '1' then specifying the block time as portMAX_DELAY will cause the task to block indefinitely (without a timeout).

Returns:

pdPASS if an item was successfully received (peeked) from the queue, otherwise errQUEUE_EMPTY.

Example usage:

1struct AMessage
2{
3 char ucMessageID;
4 char ucData[ 20 ];
5} xMessage;
6
7QueueHandle_t xQueue;
8
9// Task to create a queue and post a value.
10void vATask( void *pvParameters )
11{
12struct AMessage *pxMessage;
13
14 // Create a queue capable of containing 10 pointers to AMessage structures.
15 // These should be passed by pointer as they contain a lot of data.
16 xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
17 if( xQueue == 0 )
18 {
19 // Failed to create the queue.
20 }
21
22 // ...
23
24 // Send a pointer to a struct AMessage object. Don't block if the
25 // queue is already full.
26 pxMessage = & xMessage;
27 xQueueSend( xQueue, ( void * ) &pxMessage, ( TickType_t ) 0 );
28
29 // ... Rest of task code.
30}
31
32// Task to peek the data from the queue.
33void vADifferentTask( void *pvParameters )
34{
35struct AMessage *pxRxedMessage;
36
37 if( xQueue != 0 )
38 {
39 // Peek a message on the created queue. Block for 10 ticks if a
40 // message is not immediately available.
41 if( xQueuePeek( xQueue, &( pxRxedMessage ), ( TickType_t ) 10 ) )
42 {
43 // pcRxedMessage now points to the struct AMessage variable posted
44 // by vATask, but the item still remains on the queue.
45 }
46 }
47
48 // ... Rest of task code.
49}