c++支持coroutine的简单示例

  技术百科     |      2026-01-17 22:28

平台:linux
依赖:g++ supports c++0x

复制代码 代码如下:
void func1()
{
    coroutine.yield();
}

void func2(Coro_t co1)
{
    coroutine.resume(co1);   
    coroutine.yield();
}

void func()
{
    Coro_t co1 = coroutine.create(std::bind(&func1));   
    coroutine.resume(co1);   
    Coro_t co2 = coroutine.create(std::bind(&func2, co1));
    coroutine.resume(co2);
    coroutine.resume(co2);
}

int main()
{   
    Coro_t co = coroutine.create(std::bind(&func));
    coroutine.resume(co);
    return 0;
}