Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT


Task's Local variables

Posted by michaeln32 on 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 ?


Task's Local variables

Posted by edwards3 on 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.

Task's Local variables

Posted by richard_damon on 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.


alternate (2000 bytes)

Task's Local variables

Posted by michaeln32 on July 24, 2016

Thank You very much for your great answers.

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

define configTOTALHEAPSIZE ( ( size_t ) (15 * 1024 ) )

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


alternate (841 bytes)

Task's Local variables

Posted by rtel on July 24, 2016

configTOTALHEAPSIZE 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). http://www.freertos.org/a00111.html


alternate (872 bytes)

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS