15

There are 3 thin threads with manual low-latency context switching in the Boost:

What is the difference between Coroutine1, Coroutine2 and Fiber in Boost?

sehe
  • 374,641
  • 47
  • 450
  • 633
Alex
  • 12,578
  • 15
  • 99
  • 195
  • Coroutine is deprecated in favor of Coroutine2. Other than that, it's essentially the difference between [threads and coroutines](https://stackoverflow.com/questions/1934715/difference-between-a-coroutine-and-a-thread) – Cory Kramer Jun 13 '17 at 12:14
  • 1
    @CoryKramer Fibers aren't threads. So yeah, it's on that spectrum, but more faceted. – sehe Jun 13 '17 at 12:23
  • as a side note, for my taste the `co_await` TS is really simpler and more elegant than all of the above. – David Haim Jun 13 '17 at 12:25
  • @david I thought the proposal was for stackless coroutines, but boost had stackful? – Yakk - Adam Nevraumont Jun 13 '17 at 12:28
  • 2
    @DavidHaim as a side note to your side note, I'm really sad they went with the co_ prefix. – Borgleader Jun 13 '17 at 12:29
  • @Yakk they are different, Gor's proposal is about stackless coroutines, while boost provides stackfull coroutines. considering the latter actually requires you to write so much code **and** it uses much more memory, I simply don't see why using these in the first place – David Haim Jun 13 '17 at 12:30
  • @Borgleader I agree. but prefix or not, this feature rocks! – David Haim Jun 13 '17 at 12:31
  • 1
    @DavidHaim Stackless solve a certain subset of problems but do it more efficiently. Stackfull lets you do more insane things; I spent a semester messing around with stackful back in university, and it does have lots of power. Might not be worth the cost, and I didn't try to reimplement what I did using stackless. But my point is they are very different beasts with the same name. – Yakk - Adam Nevraumont Jun 13 '17 at 13:04
  • 1
    @David Haim, if you want yield from a deep call stack using resumable functions, **all** functions in the call stack must be resumabel functions too (what if you call thrid party, library functions in the call stack). Note that each resumable function needs memory to store local vars, registers, function state - the memory is allocated dynammically (performance?). Additionally - resumable functions benefit from compiler support, the boost implementation is library only. I haven't seen equivalent libraries using resumable functions that provide an API like boost.coroutine2 or boost.fiber. – xlrg Jun 15 '17 at 08:38

1 Answers1

44

boost.coroutine is non-C++11 and therefore requires to use a private API from boost.context (reason because it is deprecated).

boost.coroutine2 and boost.fiber require C++11 and use callcc()/continuation (implements context switch, call-with-current-continuation) from boost.context.

boost.coroutine and boost.coroutine2 implement coroutines, while boost.fiber provides fibers (== lightweigt, coroperative userland-threads, green-threads, ...) with an API similar to std::thread.

The difference between coroutines and fibers is described in N4024: Distinguishing coroutines and fibers - in short: fibers are switched by an internal scheduler while coroutines use no internal scheduler.

xlrg
  • 1,994
  • 1
  • 16
  • 14
  • I highly recommend reading the linked N4024! Sidenote: AFAIK python's asyncio does essentially that: I uses python generators (coroutines) to provide a scheduler and some fibers for asnychronous io :) – Marti Nito Apr 23 '20 at 16:08