in reply to Re: Easy coroutines?
in thread Easy coroutines?

Pardon my ignorance, but what is the benefit of using a coroutine? I checked out the "Coroutines in C" page, and from what I saw, the resulting code was a million times uglier than the initial caller/callee setup. If the point is prettier code, this seems to fail the bill. Are there other advantages like portability, efficiency, or maintainability that this addresses, or is this just another way for one to show off his skillz?

Replies are listed 'Best First'.
Re^3: Easy coroutines?
by tilly (Archbishop) on Aug 30, 2004 at 18:18 UTC
    I strongly suspect that you misread the page.

    The initial example was a caller/caller setup. That is the ideal that you'd like to achive and could with co-routines, but C doesn't have them.

    The second example was callee/callee. One half there goes with one half from the first example. The point being that changing the same code from caller to callee generally makes it a lot uglier. As the code becomes more complex, the ugliness grows rapidly.

    From there he went on to explain the hack that enabled him to partially get to the ideal. The hack is complex, and the result is not quite as clean you'd like, but you get back a long way towards the ideal. In particular the complications come from having to insert appropriate macros, but the structure of the code does not change at all.

    As for why he does this, his PuTTY program supports several different compression/encryption options with several different protocols that require parsing. If he had to rewrite one set or the other to be clean callees, that half of the code would become unmaintainable. With his hack to emulate co-routines, he keeps both halves fairly sane.

    With proper co-routines, his ideal caller/caller example is even closer to what he would get - you just need to insert "yield" at the right places in the one that you want to really be a callee.

Re^3: Easy coroutines?
by lidden (Curate) on Aug 30, 2004 at 17:37 UTC
    I don't know much of corutines but i kind of liked this page.