How do you implement Coroutines in C++

后端 未结 18 1005
刺人心
刺人心 2020-12-02 04:17

I doubt it can be done portably, but are there any solutions out there? I think it could be done by creating an alternate stack and reseting SP,BP, and IP on function entry

18条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 05:08

    This is an old thread, but I would like to suggest a hack using Duff's device that is not os-dependent (as far as I remember):

    C coroutines using Duff's device

    And as an example, here is a telnet library I modified to use coroutines instead of fork/threads: Telnet cli library using coroutines

    And since standard C prior to C99 is essentially a true subset of C++, this works well in C++ too.

提交回复
热议问题