45 ms·
An alternative approach, arguably with too much magic: http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html http://www.chiark.greenend.org.uk/~sgtatham/c
by imurray 13y ago
An alternative approach, arguably with too much magic:
http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
- nonane 13y agoActually, the link you've posted is much simpler than the parent post: no assembly voodo required and no longjump shenanigans. It's also much easier to debug under a debugger. I've successfully used this in production code. It effectively boils down to these 3 defines: #define COR_BEGIN(stateVar) switch(stateVar) { case 0:; #define COR_END(stateVar) }; stateVar = -1; #define COR_RETURN(stateVar) do { stateVar=__LINE__; return; case __LINE__:;} while (0) void dofunc(int* pstate) { COR_BEGIN(*pstate); while (computation1) COR_RETURN(*pstate); while (computation2) COR_RETURN(*pstate); COR_END(*pstate); }
- _yosefk 13y agoNo stack though, so you can't have arbitrary code running in several concurrently executing coroutines. As to debugging, I don't think setjmp/longjmp gets in the way of debuggers at all.