首页 > 代码库 > 【嵌入式程序设计】——线程管理coroutine

【嵌入式程序设计】——线程管理coroutine

非常好用的协程库,也可以当作线程管理来用

#include "coroutine.h"#include <stdio.h>struct args {    int n;};static voidfoo(struct schedule * S, void *ud) {    struct args * arg = ud;    int start = arg->n;    int i;    for (i=0;i<5;i++) {        printf("coroutine %d : %d\n",coroutine_running(S) , start + i);        coroutine_yield(S);    }}static voidtest(struct schedule *S) {    struct args arg1 = { 0 };    struct args arg2 = { 100 };    int co1 = coroutine_new(S, foo, &arg1);    int co2 = coroutine_new(S, foo, &arg2);    printf("main start\n");    while (coroutine_status(S,co1) && coroutine_status(S,co2)) {        coroutine_resume(S,co1);        coroutine_resume(S,co2);    }     printf("main end\n");}int main() {    struct schedule * S = coroutine_open();    test(S);    coroutine_close(S);        return 0;}

https://github.com/zhoudd1/coroutine

【嵌入式程序设计】——线程管理coroutine