coroutine in c 备忘

coroutine:

  stackless和stackful

  jmp

  基于switch的trick:

  http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

  文中提到高德纳另有一套办法.

  

#define crBegin static int state=0; switch(state) { case 0:
#define crReturn(x) do { state=__LINE__; return x; 
                         case __LINE__:; } while (0)
#define crFinish }

int function(void) {
    static int i;
    crBegin;
    for (i = 0; i < 10; i++)
        crReturn(i);
    crFinish;
}

  

原文地址:https://www.cnblogs.com/flytrace/p/3245501.html