fork(1) download
  1. #include <stdio.h>
  2.  
  3.  
  4. #define BEGIN switch (ctx->state) { \
  5.   case 0:
  6.  
  7. #define YIELD(val) do { \
  8.   ctx->state = __LINE__; \
  9.   return val; \
  10.   case __LINE__: \
  11.   ; \
  12.   } while (0)
  13.  
  14. #define END }
  15.  
  16. typedef struct{
  17. int state;
  18. int i;
  19. } Ctx;
  20.  
  21. int fibs(Ctx* ctx)
  22. {
  23. int x;
  24. BEGIN;
  25. YIELD(++ctx->i);
  26. YIELD(++ctx->i);
  27.  
  28. END;
  29. return 0;
  30. }
  31.  
  32. int main()
  33. {
  34. int i=0;
  35.  
  36. Ctx t0 = {0,1};
  37. Ctx t1 = {0,101};
  38. while (i = fibs(&t0)) {
  39. printf("t0:%d\n", i);
  40. printf("t1:%d\n", fibs(&t1));
  41. }
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 5428KB
stdin
Standard input is empty
stdout
t0:2
t1:102
t0:3
t1:103