Quality RTOS & Embedded Software

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




Loading

How to traverse a queue

Posted by caguilar10 on May 25, 2016

Hi Forum I have an application where I have to traverse a full or semi-full queue. I was thinking of retrieving the number of elements in the queue using uxQueueMessagesWaiting, using a for loop to traverse and peek (xQueuePeek) the queue element for a specific value match and remove it from the queue with xQueueReceive() if it matches.
The question that I have is if xQueuePeek actually moves from element to element after subsequent calls. Thanks. CA


How to traverse a queue

Posted by rtel on May 25, 2016

No - xQueuePeek() will always peek the front of the queue. There is no API that will allow you to traverse the queue, although one could be created.

Internally, and hidden from the user, the contains a 'storage area' which is just a circular buffer. The queue then has members pointing to the front and back. If you know the queue item size (also stored in the queue structure) then you could traverse the storage area yourself.

If execution time, or ordering in the queue, is of no concern then you can always read an item from the queue, see if it is what you want, and post it back to the end of the queue if it is not what you want. That will be quite a heavy way of doing it though.

However, from the description of what you want to do I think a queue might not be the right primitive for you anyway. FreeRTOS includes a doubly linked list implementation, which is used internally rather than being part of the public API, but the functions are actually available to the user (not documented as API functions though). That might be a better option.


How to traverse a queue

Posted by caguilar10 on May 25, 2016

Thanks for the quick reply. 1- Regarding the double linked list. This is just the bare-bones list implementation, right? If I want a similar implementation, I would need to add a semaphore to block like the queues and implement any functionality that queues provide but list don't, right? 2- This particular functionality is not time-sensitive. So I could remove the item with xQueueReceive and if not matching, put it back in the back of the queue. If I traverse the whole queue this way, the queue element order will still be preserved minus the events that were removed.


How to traverse a queue

Posted by rtel on May 25, 2016

1- Regarding the double linked list. This is just the bare-bones list implementation, right? If I want a similar implementation, I would need to add a semaphore to block like the queues and implement any functionality that queues provide but list don't, right?

Correct, unlike a queue/semaphore/etc. the linked list is bare bones with no thread safety built in.

2- This particular functionality is not time-sensitive. So I could remove the item with xQueueReceive and if not matching, put it back in the back of the queue. If I traverse the whole queue this way, the queue element order will still be preserved minus the events that were removed.

Correct.


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




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

Latest News

FreeRTOS kernel V10 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