Hi,
Why aren’t there functions to query whether a queue is empty/full?
While working with queues I wanted to fill one of them before being processed, but my program crashed. This problem has been around for a while (Check if queue is empty! - #13 by system) and FreeRTOS hasn’t add since then a single QueueIsEmpty() or QueueIsFull() queries. This next code, although logically correct, doesn’t work:
void producer_task( void* pvParameters )
{
int data = 10;
Serial.println( pcTaskGetName( NULL ) );
while( 1 )
{
vTaskDelay( pdMS_TO_TICKS( 500 ) );
while( uxQueueSpacesAvailable( g_queue_handler ) > 0 ){
if( xQueueSend( g_queue_handler, (void*) &data, pdMS_TO_TICKS( 100 ) ) == pdFALSE ) {
Serial.print( "TO(W): " );
}
data += 10;
vTaskDelay( pdMS_TO_TICKS( 10 ) );
// I simulate a delay on filling the queue
}
}
}
I already know that I can use the xQueueSend()
return value to determine when the queue is full but, What if I only want to know whether the queue is full, without doing anything else? There are cases in which one needs to fill the queue before it’s used.
This next code works partially, but it is not what I want:
void producer_task( void* pvParameters )
{
int data = 10;
Serial.println( pcTaskGetName( NULL ) );
while( 1 )
{
vTaskDelay( pdMS_TO_TICKS( 500 ) );
while( xQueueSend( g_queue_handler, (void*) &data, 0 ) == pdTRUE ){ // I got stuck in this loop
data += 10;
vTaskDelay( pdMS_TO_TICKS( 10 ) );
}
}
}
void consumer_task( void* pvParameters )
{
Serial.println( pcTaskGetName( NULL ) );
while( 1 )
{
int data;
while( xQueueReceive( g_queue_handler, &data, pdMS_TO_TICKS( 600 ) ) == pdTRUE ){
Serial.println( data );
}
}
}
I could also use a for loop since I-fill-then-empty the queue:
void producer_task( void* pvParameters )
{
QUEUE_TYPE data = 10;
Serial.println( pcTaskGetName( NULL ) );
while( 1 )
{
for( uint8_t i = 0; i < QUEUE_ELEMS; ++i ){
if( xQueueSend( g_queue_handler, (void*) &data, pdMS_TO_TICKS( 100 ) ) != pdFALSE ){
data += 10;
vTaskDelay( pdMS_TO_TICKS( 10 ) );
// I simulate a delay on filling the queue
} else{ // timeout:
Serial.println( "TO(W)" );
}
}
vTaskDelay( pdMS_TO_TICKS( 500 ) );
}
}
But under this schema, the functions uxQueueSpacesAvailable()
and uxQueueMessagesWaiting()
what are good for?
Thank you!