Task's Local variables

michaeln32 wrote on Saturday, July 23, 2016:


I am using MCU STM32F205RC.
Compiler: I am using Atollic
FreeRTOS V8.2.1

1.Is a local variable of task remains his value after context switch to another task and back or the local variable of a task reinitilized to his original value ?

2.Is there any common sense to define local variable of a task as static ?

  1. Can you please tell me how can i know what is the total maximum size in bytes of tasks stacks ?


edwards3 wrote on Saturday, July 23, 2016:

FreeRTOS is a proper preemptive multi tasking system so each task is like its own program. Tasks dont know anything about context switches, they don’t start and stop so variables always have the same value, there is no point at which they could reset to their init value.

richard_damon wrote on Saturday, July 23, 2016:

The one thing that has problems with automatic (stack based) variables retaining values would be inside co-routines which are a very striped down version of tasks for use in very memory limited machines. As MEdwards states, tasks have no problem retaining values over a context switch.

A Task would have problems remembering values if the task ends and gets restarted, then variables on the stack would likely lose their value.

As far as when to use the stack and when to use local static variables, if the function is used for only 1 task, it makes little difference. If the function is used for multiple tasks, like the function supports multiple devices in the system, and the parameter passed on task startup defines which one that task is handling, then stack variable would be distinct for each class, while a static variable would be shared.

Which is better? Using stack variables makes it easier if later you want to reuse the function for a second task. Static variables make it easier for sub-functions to access the values.

As to the maximum size of the tasks stack, one answer is how much you allocated, as that is the most it is allowed to use. As to how much it will use, some compilers have an option to try and compute the stack usage for a function (and thus a task), others do not, and you would have to estimate/count how much stack is used for variables and add for parameter/function calls and other overhead.

michaeln32 wrote on Sunday, July 24, 2016:

Thank You very much for your great answers.

the next define is defined in my FreeRTOSConfig.h header file :

define configTOTAL_HEAP_SIZE ( ( size_t ) (15 * 1024 ) )

Is this define represent the maximum bytes that all the tasks in the system can allocat to them selfs ?

rtel wrote on Sunday, July 24, 2016:

configTOTAL_HEAP_SIZE is the ‘TOTAL’ available in the entire system -
not to each task individually. An individual task can allocate as much
as it likes until the heap is exhausted (no free heap available).