c - 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] {{..},{..}}
为什么这么复杂?
解决方案
推荐阅读
- java - 为什么在外部可运行类中看不到我的公开活动?
- java - 如果数据中有一些特殊符号,则从数据库中检索数据包含一些字符
- swift - 将函数映射到快速错误中的全局变量“初始化程序'init(_:)'要求'[_]'符合'LosslessStringConvertible'”
- zabbix - 在维护模式期间不显示某些触发严重性
- python-3.x - 如何计算总数。字典中的键?
- javascript - Vuejs在JSX中绑定变量与范围
- apache-calcite - Calcite 的 ConverterRule 和 RelOptRule 有什么区别?
- c# - 通过值列表查找最短路径
- mysql - 每次创建表列并点击保存时都会出错
- corda - Corda - 可以在生产环境中部署使用 Corda V1.0 构建的 CorDapp 吗?