分类: C/C++
2008-06-10 22:16:30
#define _WIN32_WINNT 0x400 #include #include #define FIBER_COUNT 10 void *fiber_context[FIBER_COUNT]; VOID WINAPI fiberProc(void*); int main() { int i; int fibers[FIBER_COUNT]; for (i = 0; i < FIBER_COUNT; i++) fibers[i] = i; fiber_context[0] = ConvertThreadToFiber(NULL); for (i = 1; i < FIBER_COUNT; ++i) { fiber_context[i] = CreateFiber(0, fiberProc, &fibers[i]); if (fiber_context[i] != NULL) printf("Fiber %d created\n", i); } for (i = 1; i < FIBER_COUNT; ++i) { if (fiber_context[i] != NULL) SwitchToFiber(fiber_context[i]); } printf("Over\n"); } void WINAPI fiberProc(void *fiber_nbr) { int nbr; nbr = *((int*)fiber_nbr); printf("Hello from fiber %d\n", nbr); SwitchToFiber(fiber_context[0]); } |
Fiber 1 created Fiber 2 created Fiber 3 created Fiber 4 created Fiber 5 created Fiber 6 created Fiber 7 created Fiber 8 created Fiber 9 created Hello from fiber 1 Hello from fiber 2 Hello from fiber 3 Hello from fiber 4 Hello from fiber 5 Hello from fiber 6 Hello from fiber 7 Hello from fiber 8 Hello from fiber 9 Over |