本项目是一个简易的协程库,所复现原文如下
https://developer.aliyun.com/article/52886
添加了更加详细的注释,修改了uthread_create和uthread_resume函数,使输出符合预期
原来的uthread_create函数会在协程创建时就把它加入运行队列,因此需要移除下面两行
schedule.running_thread = id;
swapcontext(&(schedule.main), &(t->ctx));
在原来的uthread_resume函数中修改if判断语句(因为新创建的协程状态为RUNNABLE)
if (t->state == SUSPEND||t->state==RUNNABLE) {
schedule.running_thread = id; // 记录即将切换协程的id
t->state = RUNNING; // 把协程状态改为运行
swapcontext(&(schedule.main),&(t->ctx));
}
修改后程序将正确输出
1
11
111
1111
main
----------------
22
22
3333
3333
22
22
3333
3333
main over