Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Code Provided for a vQueueFlush function

Posted by D-Kure on June 18, 2010
Hello All,

I recently needed a queue flush function, and it seemed like a waste to use a while(xQueueReceive()) loop to flush the queue.

Instead I came up with the following function

void vQueueFlush( const xQueueHandle pxQueue )
portBASE_TYPE xYield = pdFALSE;


/* Reset the queue to default state */
pxQueue->uxMessagesWaiting = 0;
pxQueue->pcWriteTo = pxQueue->pcHead;
pxQueue->pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - 1 ) * pxQueue->uxItemSize );

/* Clear the event list waiting to send */
while (listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
/* The task waiting has a higher priority than this task. */
xYield = pdTRUE;

if (xYield == pdTRUE) {



Now I have spotted some simlilar code by Richard Barry which also mentioned rxLocks and txLocks. I have not touched these as I am not sure what they do exactly. I have tested the code, and it works. It also saves real time when trying to use large queues. I created a queue of 5000 elements and when using this queue free function it saved 16ms of execution on an STM32 @ 72MHz.

It would be great if this could be included in the next version of FreeRTOS.

Regards Paul

RE: Code Provided for a vQueueFlush function

Posted by Dave on June 18, 2010
Can you post a link to this message in the feature request tracker?

I presume for a complete implementation you would also need to clear the tasks waiting to receive.

RE: Code Provided for a vQueueFlush function

Posted by D-Kure on June 18, 2010
As this is a flushing of the queue, I think that tasks that are waiting to receive still need to be blocked, waiting on data.

Its meant to flush the queue of all data, not necessarily reset the queue back to the default state.

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Meet us at Embedded World. Hall 3A-525.

Hear from Richard Barry at Embedded World. Feb 28, 16:00, Hall 4-428.

Video: Watch James Gosling & Richard Barry at re:Invent, Las Vegas 2017.

FreeRTOS kernel V10.0.1 is available for immediate download. Now MIT licensed.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS