Page 1 of 1

Proper Use of Local (Auto) Variables in Salvo Tasks

PostPosted: Fri Feb 28, 2003 6:38 am
by aek
It has come to my attention via a (disgruntled, unfortunately for us) Salvo user that we may not be doing a good enough job of explaining to our users how local (auto) variables can be used in Salvo tasks.

From the FAQ:

Why must tasks use static variables?
Static variables are assigned their own unique address in RAM, and may not be visible to other tasks. By declaring a task's variables as static you are guaranteeing that they will remain unchanged while the task is not running. This is the only way to preserve the variable from one context switch to the next. If the variable were not static (i.e. if it were an auto variable) it's likely that it would be changed by other tasks, functions or ISRs, and unpredictably.

It is safe to use auto variables in tasks as long as the task does not require that the value of the variable be maintained in the task from one context switch to the next. For example, if a simple for() loop is used to repeatedly call a function, and then the task context switches, as long as the loop index is initialized each time, it should not pose a problem.

int i;

for (;;) {
for ( i = 0; i < 5 ; i++ ) {

Please read and understand this so that your use of auto variables in tasks does not cause runtime problems in your Salvo applications.