LGT 发表于 2013-11-6 10:27:53

一个简单的程序解释protothread的原理

本帖最后由 LGT 于 2013-11-6 10:29 编辑

看到smset推荐的protothread, 感觉挺有意思, 看了下源代码, 把原理用一段简单的C代码解释一下,
希望对还不是很了解的同学有帮助:#include <stdio.h>

void thread_fsm(char *tname, unsigned char *status)
{
        switch(*status) { case 0: // PT_BEGIN
                while(1) {
                        printf("%s: Here is step 0!\n", tname);
                        if('q' != getchar())
                                return;
                        *status = __LINE__; case __LINE__:// must be kept in same line, or the two __LINE__ will not equal
                        printf("%s: Here is step 1!\n", tname);
                        if('q' != getchar())
                                return;
                        *status = __LINE__; case __LINE__:
                        printf("%s: Here is step 2!\n", tname);
                        if('q' != getchar())
                                return;
                        printf("%s: Now return to step 0!\n", tname);
                        *status = 0;
                        break;
                        default:
                                printf("%s: Got wrong status!\n", tname);
                }
        }       
}

int main(int argc, char *argv[]) {
       
        unsigned char status1 = 0;        // PT_INIT
        unsigned char status2 = 0; // PT_INIT
       
        while(1) {
                thread_fsm("thread 1", &status1);
                thread_fsm("thread 2", &status2);
        }
       
}运行上面的代码,输入q+回车,就可以看到thread的执行了。
thread的写法如果变的规范些, 然后把__LINE__替换成具体的数值,就更容易懂了:void thread_fsm(char *tname, unsigned char *status)
{
        switch(*status) {
                case 0: // PT_BEGIN
                        while(1) {
                                printf("%s: Here is step 0!\n", tname);
                                if('q' != getchar())
                                        return;
                                *status = 1;
                case 1:
                        printf("%s: Here is step 1!\n", tname);
                        if('q' != getchar())
                                return;
                        *status = 2;
                case 2:
                        printf("%s: Here is step 2!\n", tname);
                        if('q' != getchar())
                                return;
                        printf("%s: Now return to step 0!\n", tname);
                        *status = 0;
                        break;
                default:
                        printf("%s: Got wrong status!\n", tname);
                }
        }       
}

smset 发表于 2013-11-6 13:10:44

{:smile:} protothread 本质上确实就是状态机,只是在语法上模仿了线程的写法。

也只有在C语言里,才能使用这种写法。

richards 发表于 2013-11-6 13:26:39

嗯嗯   还是有用

kinsno 发表于 2013-11-6 13:50:14

本帖最后由 kinsno 于 2013-11-6 13:51 编辑

smset 发表于 2013-11-6 13:10 static/image/common/back.gif
protothread 本质上确实就是状态机,只是在语法上模仿了线程的写法。

也只有在C语言里,才能使 ...

1、SWITCH基本上就不要想用了,清一水的IF吧,这个不太好,整个程序封杀了SWITCH,这个也让人不爽。
2、虽然有定时器和信号阻塞,但是只能在任务函数里用,在底层,或底底层或驱动层,它没法用了;这个让人不爽。
归根到底:有这两种限制,倒不如说是个时间片轮转。
PS:兄弟,能不能整一个GOTO版本啊。用了GOTO版本至少解决了switch,但是第2个问题,不好弄了,有建议不?

vows 发表于 2013-11-6 23:27:11

巧妙的用了c的语法啊
页: [1]
查看完整版本: 一个简单的程序解释protothread的原理