How to traverse a queue

caguilar10 wrote on Wednesday, 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

rtel wrote on Wednesday, 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.

caguilar10 wrote on Wednesday, 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.

rtel wrote on Wednesday, 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.