Queues in FreeRTOS with Arduino

I am new to freeRTOS and i am trying to learn synchronization, i have 2 tasks that i am trying to synchronize with queues. Both tasks have the same priority, one to send 100 values and the other to receive the 100 values, i am trying to send every 50ms and receive every 20ms for example. This the code i have done for now :

void Prod(void* pvParameters)
{  
  while(1)
  {
    for(idx = 0; idx < N; idx++){
      xQueueSend(queueHandle, &idx, 100/portTICK_PERIOD_MS);
    }

    vTaskDelay(50/portTICK_PERIOD_MS);
  }
}

void Cons(void* pvParameters)
{  
  while(1)
  {
    for(idx = 0; idx < N; idx++){
      xQueueReceive(queueHandle, &element, portMAX_DELAY);
      Serial.println(element);
    }

    vTaskDelay(20/portTICK_PERIOD_MS);
  }
}

The output is not what i really desire, it receives only 13 values so it display from 0 to 12. i don’t really know why. Any help would be appreciated. Thanks in advance

We would need to see how the queue is created and know the value of N.

Hello this is what i am using to create the queue : queueHandle = xQueueCreate( queueSize, sizeof( int ) ) with queueSize = 100 and N = 100

Looking at the code, I don’t see a declaration for idx, would it happen to be a global that the tasks are sharing? (Bad global, bad global)

You are right it is a global variable. but i just tried it with different variables and still the same thing i can not recover the whole values in the queue.

And… check the return codes (here: Q send/receive) to get an idea what’s going on :wink:

How can i check the return codes please ?

Why when i added a delay after the Serial print it worked ?

As documented here Queue Management

        /* Send an unsigned long.  Wait for 10 ticks for space to become
        available if necessary. */
        if( xQueueSend( xQueue1,
                       ( void * ) &ulVar,
                       ( TickType_t ) 10 ) != pdPASS )
        {
            /* Failed to post the message, even after 10 ticks. */
        }
1 Like