50 行代码写一个迷你无栈协程框架
pluvet2024-07-22 23:360
玩具项目,不过真的很好玩!
```c
int prime_factor_co(Co *co, void *data)
{
PrimeFactorData *pfData = (PrimeFactorData *)data;
CO_BEGIN(co);
pfData->current = 2;
while (pfData->number > 1)
{
while (pfData->number % pfData->current == 0)
{
pfData->number /= pfData->current;
pfData->factor = pfData->current;
CO_YIELD(co, pfData->factor);
}
pfData->current++;
CO_YIELD(co, 0); // Yield to indicate progress
}
CO_END(co);
return 0;
}
```
https://zhuanlan.zhihu.com/p/710329144
https://github.com/pluveto/minico
```c
int prime_factor_co(Co *co, void *data)
{
PrimeFactorData *pfData = (PrimeFactorData *)data;
CO_BEGIN(co);
pfData->current = 2;
while (pfData->number > 1)
{
while (pfData->number % pfData->current == 0)
{
pfData->number /= pfData->current;
pfData->factor = pfData->current;
CO_YIELD(co, pfData->factor);
}
pfData->current++;
CO_YIELD(co, 0); // Yield to indicate progress
}
CO_END(co);
return 0;
}
```
https://zhuanlan.zhihu.com/p/710329144
https://github.com/pluveto/minico
pluvet
主题数 56 | 帖子数 2871 | 注册排名 2 |