this image comes from Practical usage of setjmp and longjmp in C.
From my understanding, the coroutine
is two process looks like doing parallelly for human but actually doing a single process for machine.
But using setjmp
& longjmp
I feel very hard to read the code. If need to write the same one. For example process A & B, I will give serval States
to the processes to split them into different pieces(states),
do sequentially like:
Process A
switch (state)
case A1:
if (A1 is done)
do B1
break;
...
Process B
switch (state)
case B1:
if (B1 is done)
do A2
break;
...
I need a reason to support me use setjmp
& longjmp
& coroutine
in C/C++.
What's advantage?
setjmp/longjmp()
is rarely used by nowaday programmer. Instead, you should use more powerful boost::coroutine, or my QtNetworkNg. Coroutine is used widely, and mostly, in network programming.I am the author of QtNetworkNg which provides a stackful coroutine implementation. In the document of QtNetworkNg, I wrote:
Besides of that, coroutines can handle state machine and timeline more cleanly. Some online game server use coroutine to handle the interacting between thousands of peers.