Global variables and FreeRTOS

Hi,
I’m beginner with FreeRTOS and I have a question about using global variables. I have two funcions in task. I need to transfer some data (flag) between two functions. Can global variables be used within a single task? Example:

void myTask(void *pvParameters)
{
for( ; ; )
{
myFunction1();
myFunction2();
}
}

Another file mySource.c

uint8_t flag = 0;

void myFunction1(void)
{
flag = 1;
}

void myFunction2(void)
{
if(flag == 1)
{
//do something
}
}

There are many ways for tasks to communicate - the simplest of which is just to use a global variable, so yes you can do this. You need to be careful about thread safety though if both tasks try writing to the variable at the same time then only one write will stick, or worse, if the variable cannot be accessed in one write operation one task may write the top bytes of the variable and another task the bottom bytes of a variable.

If you only have one writer then you should be ok - provided the variable can be written to in one operation. For example, if you have a 32-bit value on a 32-bit machine then all 32-bits will get written in one go. If you have a 32-bit value on an 8 or 16-bit machine then it will take multiple writes to update all 32-bits and there is a risk that another task may read the variable when only some of its 4-bytes have been updated.

Within a single task there is no problem using global or static variables. As Richard explained think twice when doing so from multiple tasks. You might stumble into problems when using some functions in an other task one day not longer having in mind they use global or static variables internally.