protothread学习

来源:互联网 发布:手机移动网络dns劫持 编辑:程序博客网 时间:2024/05/22 08:01

转自:http://blog.csdn.net/finewind/article/details/6186291
这里讲代码备份供参考:

#ifndef PC_H#define PC_Htypedef unsigned int INT16U;struct pt{  INT16U lc;  };#define PT_THREAD_WAITING   0#define PT_THREAD_EXITED    1//初始化任务变量,只在初始化函数中执行一次就行#define PT_INIT(pt)     (pt)->lc = 0//启动任务处理,放在函数开始处#define PT_BEGIN(pt)    switch((pt)->lc) { case 0:// 等待某个条件成立,若条件不成立则直接退出本函数,下一次进入本函数就直接跳到这个地方判断  // __LINE__ 编译器内置宏,代表当前行号,比如:若当前行号为8,则 s = __LINE__; case __LINE__: 展开为 s = 8; case 8:#define PT_WAIT_UNTIL(pt,condition)   (pt)->lc = __LINE__;   case __LINE__: /                                      if(!(condition))  return             // 结束任务,放在函数的最后#define PT_END(pt)      } // 等待某个条件不成立      #define PT_WAIT_WHILE(pt,cond)    PT_WAIT_UNTIL((pt),!(cond)) // 等待某个子任务执行完成#define PT_WAIT_THREAD(pt,thread)   PT_WAIT_UNTIL((pt),(thread))  // 新建一个子任务,并等待其执行完退出#define PT_SPAWN(pt,thread) /  PT_INIT((pt));            /  PT_WAIT_THREAD((pt),(thread))  // 重新启动某任务执行#define PT_RESTART(pt)  PT_INIT(pt); return // 任务后面的部分不执行,直接退出#define PT_EXIT(pt)     (pt)->lc = PT_THREAD_EXITED;return #endif//---------------------static struct pt pt1,pt2;static int protothread1_flag,protothread2_flag;// ========================================// 线程1// ========================================static void protothread1(struct pt *pt){  PT_BEGIN(pt); // 开始时调用   while(1)  {    // 应用代码    protothread1_flag = 1;    PT_WAIT_UNTIL(pt,protothread2_flag != 0); // 等待protothread2_flag 标志置位    protothread2_flag = 0;  }    PT_END(pt);  // 结束时调用}// ========================================// 线程2// ========================================static void protothread2(struct pt *pt){  PT_BEGIN(pt);  while(1)  {    // 应用代码    protothread2_flag = 1;    PT_WAIT_UNTIL(pt,protothread1_flag != 0); // 等待protothread1_flag 标志置位    protothread1_flag = 0;  }  PT_END(pt);}// ========================================// 主函数// ========================================void main(void){  PT_INIT(&pt1);  // 初始化  PT_INIT(&pt2);    while(1)  {    protothread1(&pt1);    protothread2(&pt2);  }}//---------------// ========================================// 线程1// ========================================static void protothread1(struct pt *pt){  // PT_BEGIN(pt);展开  switch(pt->lc)  {    case 0:    ;            while(1)    {      protothread1_flag = 1;            // PT_WAIT_UNTIL(pt,protothread2_flag != 0);展开      // 条件判断部分,条件不成立,则调度      pt->lc = 26;   // 假定当前为26行      case 26:      if(protothread2_flag == 0)        return;      // 若protothread2_flag未发生,返回      protothread2_flag = 0;    }    // PT_END(pt); 对应switch  }}// ========================================// 线程2// ========================================static void protothread2(struct pt *pt){  switch(pt->lc)  {    case 0:    ;               while(1)    {      protothread2_flag = 1;      pt->lc = 44;      case 44:      if(protothread1_flag == 0)        return;            myFunc2();      protothread1_flag = 0;    }  }}

更多内容请参考原帖。


0 0
原创粉丝点击