首页 > 解决方案 > RTOS 任务调度。为什么在简单的循环调度程序中使用函数指针而不是数组?

问题描述

使用函数指针指向对二维数组进行操作的 void 函数,而不是操作常量数组。

在 Rob Williams 所著的《实时系统开发》一书中,第 4.7 章介绍了一个简单的任务调度程序。

定义了任务列表:task_one()、task_two() 等。

这个想法是建立一个二维的任务数组,并以恒定的频率遍历表,以便一个接一个地执行任务。

这是示例代码:

#include ...

//time related variables
var1, var2
//no explicit 2-D table declaration/definition

//Tasks
void task_one() {
 ...
}

void task_two() {
 ...
}

void task_idle() {
 ...
}

//Here is the interesting part - the scheduler; why is this a function pointer (a strange one, though) and not a standard const array?
void (*ttable[2][4])() = {
 {task_one, task_two, task_idle, task_idle},
 {task_two, task_two, task_idle, task_idle}
}

int main() { 
 while (1) {
  for (slot = 0; slot < 2; slot ++) {
   for (cycle = 0; cycle < 4; cycle ++) {
    (*ttable[slot][cycle])(); // dispatch next task
   }
   //wait for a timer to jump to the next task
  }
 }
}

一切都是在编译时定义的。我希望调度程序是

const tab[2][4] {{..},{..}}

为什么这么复杂?

标签: crtos

解决方案


推荐阅读