How do you implement Coroutines in C++

后端 未结 18 958
刺人心
刺人心 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条回答
  •  爱一瞬间的悲伤
    2020-12-02 04:59

    You should always consider using threads instead; especially in modern hardware. If you have work that can be logically separated in Co-routines, using threads means the work might actually be done concurrently, by separate execution units (processor cores).

    But, maybe you do want to use coroutines, perhaps because you have an well tested algorithm that has already been written and tested that way, or because you are porting code written that way.

    If you work within Windows, you should take a look at fibers. Fibers will give you a coroutine-like framework with support from the OS.

    I am not familiar with other OS's to recommend alternatives there.

提交回复
热议问题