Hi. I have two tasks running, one that is altering a variable when an event comes and the other that is checking its value. in the second task where i have no delay or yield, i am seeing that the value of the variable doesn’t alter. Adding a vTaskDelay of 1-2 ms solves the problem. Can someone explain why this would happen? The following is a pseudo code but the concept illustrates the real problem.
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>
/* Shared variable */
int sharedValue = 0;
/*-----------------------------------------------------------*/
/* Task that modifies the sharedValue every 10 ms */
void vModifierTask(void *pvParameters)
{
(void)pvParameters;
for (;;)
{
if (event == NETWORK_DATA_RECEIVED)
{
printf("Network data received\n");
sharedValue = 5;
}
// sharedValue++;
vTaskDelay(pdMS_TO_TICKS(10)); /* runs every 10 ms */
}
}
/*-----------------------------------------------------------*/
/* Task that continuously checks sharedValue (no delay) */
void vCheckerTask(void *pvParameters)
{
(void)pvParameters;
for (;;)
{
/* Poll the value continuously */
if (sharedValue > 0)
{
printf("sharedValue = %d\n", sharedValue);
}
/* No delay or yield here! */
}
}
/*-----------------------------------------------------------*/
int main(void)
{
/* Create both tasks at the same priority */
xTaskCreate(vModifierTask, "Modifier", configMINIMAL_STACK_SIZE, NULL, 0, NULL);
xTaskCreate(vCheckerTask, "Checker", configMINIMAL_STACK_SIZE, NULL, 0, NULL);
/* Start the scheduler */
vTaskStartScheduler();
/* Should never reach here */
for (;;);
return 0;
}