[程序员] cc_task: 用 C 语言写 async/await 协程,还有闭包
https://github.com/ivan7wl/cc_task cc_task 是一个 C 语言协程库,用 async/await 的方式写协程,还可以用闭包。 协程是无栈的,跨恢复点的变量保存在闭包中,非常的轻量,大部分场景下一条 64B CPU cache line 就能放得下。同时也支持有栈协程。 #include "cc_task.h" #include <stdio.h> int main(void) { int c = 10; // task_b captures c and uses it as task state cc_task_t *task_b = cc_task_new(CC_TASK(t) { CC_BEGIN(t); printf("B: c = %d\n", ++$(c)); CC_YIELD(t); printf("B: c = %d\n", ++$(c)); CC_END(t, c); }); cc_task_t *task_a = cc_task_new(CC_TASK(t) { CC_BEGIN(t); printf("A: await B ...\n"); cc_uintptr code; CC_AWAIT(t, task_b, code); printf("A: B end, code = %llu\n", (unsigned lon
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.