【1】什么是可重入函数和不可重入函数呢?
可重入的函数:一般是保存在栈里面的,是可以被编译器随机的分配内存并且释放的函数称为可重入函数
不可重入函数:一般是指函数返回值是static 型的或者是函数内部定义了static变量或者使用了全局变量等称为不可重入函数
【2】为什区分可重入和不可重入函数呢?
因为多任务操作系统中,需要一个函数要满足同时被多个任务调用,而且要确保每个任务都能单独的维护自己的栈空间或者自身在内存寄存器中的值
【3】怎样识别函数和不可重入函数呢?
/*This will either be
passed on the stack or in a CPU register. Either way is safe as
each task maintains its own stack and its own set of register
values. */
long int handler(int var1) { int var2; var2 = var1 + 2; return var2; } //可重入函数
/* In this case lVar1 is a global variable so every task that calls
the function will be accessing the same single copy of the variable. */
long var1 long int handler(void) {
/* This variable is static so is not allocated on the stack. Each task
that calls the function will be accessing the same single copy of the
variable. */
static long state1 = 0; long 1Return; switch(state1) { case 0: 1Return = state1+10; state1 = 1; break; case 1: 1Return = state1+20; state1 = 0; break; } } //不可重入函数