Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Coroutine, Coroutine2 and Fiber?

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

  • Boost.Coroutine: http://www.boost.org/doc/libs/1_64_0/libs/coroutine/doc/html/index.html
  • Boost.Coroutine2: http://www.boost.org/doc/libs/1_64_0/libs/coroutine2/doc/html/index.html
  • Boost.Fiber: http://www.boost.org/doc/libs/1_64_0/libs/fiber/doc/html/index.html

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

like image 711
Alex Avatar asked Jun 13 '17 12:06

Alex


People also ask

Are fibers coroutines?

Fibers (sometimes called stackful coroutines or user mode cooperatively scheduled threads) and stackless coroutines (compiler synthesized state machines) represent two distinct programming facilities with vast performance and functionality differences.

What is the difference between Coroutine and thread?

Coroutines are very similar to threads. However, coroutines are cooperatively multitasked, whereas threads are typically preemptively multitasked. Coroutines provide concurrency but not parallelism.

What is boost fiber?

Fiber provides a framework for micro-/userland-threads (fibers) scheduled cooperatively. The API contains classes and functions to manage and synchronize fibers similiarly to standard thread support library.

What is boost Coroutine?

Boost. Coroutine provides templates for generalized subroutines which allow multiple entry points for suspending and resuming execution at certain locations. It preserves the local state of execution and allows re-entering subroutines more than once (useful if state must be kept across function calls).


1 Answers

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.

like image 145
xlrg Avatar answered Sep 19 '22 06:09

xlrg