开源项目 coroutine
使用教程
coroutineA asymmetric coroutine library for C.项目地址:https://gitcode.com/gh_mirrors/co/coroutine
1. 项目的目录结构及介绍
coroutine/
├── Makefile
├── README.md
├── coroutine.c
├── coroutine.h
├── example_channel.c
├── example_pingpong.c
├── example_simple.c
├── example_socket.c
└── schedule.c
- Makefile: 用于编译项目的Makefile文件。
- README.md: 项目说明文档。
- coroutine.c: 协程实现的核心代码。
- coroutine.h: 协程的头文件,包含API定义。
- example_channel.c: 使用协程通道的示例代码。
- example_pingpong.c: 协程乒乓示例代码。
- example_simple.c: 简单的协程示例代码。
- example_socket.c: 使用协程处理socket的示例代码。
- schedule.c: 协程调度器的实现代码。
2. 项目的启动文件介绍
项目的启动文件主要是示例代码文件,如 example_simple.c
、example_channel.c
等。这些文件展示了如何使用协程库进行编程。以下是 example_simple.c
的简要介绍:
#include "coroutine.h"
#include <stdio.h>
struct args {
int n;
};
static void
foo(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);
}
}
int
main() {
struct schedule * S = coroutine_open();
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");
coroutine_close(S);
return 0;
}
3. 项目的配置文件介绍
该项目没有显式的配置文件,所有的配置和初始化都在代码中完成。例如,协程的创建和调度在 example_simple.c
中通过调用 coroutine_open()
、coroutine_new()
和 coroutine_resume()
等函数进行。
coroutineA asymmetric coroutine library for C.项目地址:https://gitcode.com/gh_mirrors/co/coroutine