C++ supports a simple example of coroutine

  • 2020-04-02 02:16:55
  • OfStack

Platforms: Linux
Dependency: 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;
}


Related articles: