78

Today I was reading several articles on the Internet about fibers, coroutines and green threads, and it seems like these concepts have very much in common, but there are slight differences, especially when we talk about fibers and coroutines.

Is there a concise, correct summary of what makes them different from each other?

Update: I find the Distinguishing coroutines and fibers (N4024 C++ draft) document particularly good at differentiating between fibers and coroutines.

DejanLekic
  • 913
  • 1
  • 7
  • 8

1 Answers1

131

A Fiber is a lightweight thread that uses cooperative multitasking instead of preemptive multitasking. A running fiber must explicitly "yield" to allow another fiber to run, which makes their implementation much easier than kernel or user threads.

A Coroutine is a component that generalizes a subroutine to allow multiple entry points for suspending and resuming execution at certain locations. Unlike subroutines, coroutines can exit by calling other coroutines, which may later return to the point where they were invoked in the original coroutine.

A Green Thread is a thread that is scheduled by a virtual machine (VM) instead of natively by the underlying operating system. Green threads emulate multithreaded environments without relying on any native OS capabilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support.

Fibers and green threads both rely on a separate scheduler to select the next fiber or thread, but a fiber must explicitly cede control (yield) to its scheduler. A green thread will run until interrupted ("preempted") by its scheduler.

Fibers and coroutines both involve "yielding" in which the fiber/coroutine decides when to relinquish control - this is cooperative multitasking. Whereas the fiber always yields to its scheduler, a coroutine decides for itself who to yield to.

Coroutines can be used to implement fibers by always yielding to a scheduler coroutine. Fibers can be used to implement coroutines by allowing each fiber to communicate to the scheduler which fiber should be run when it yields.

eenblam
  • 103
  • 4
Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • 18
    I can only say for myself - this answer is very valuable to me. I share opinion with Robert on this. – DejanLekic Aug 22 '14 at 15:16
  • "Summaries of broad topics are specialty of Wikipedia, which is focused and optimized on providing this content and fostering a culture oriented on it. People willing, best capable of and experienced in providing such summaries go to Wikipedia, not to Stack Exchange.... People looking for this in search engine would be typically presented two directly competing content examples, one at SE network, another at Wikipedia... Due to reasons mentioned above, it is most likely that SE content would look inferior in comparison..." – gnat Aug 22 '14 at 15:39
  • 1
    Note, coroutines and fibers could be considered quite closely related -- possibly even the same thing. If one wanted coroutines, they can be implemented via fibers with very little effort, and vice versa. – cHao Aug 23 '14 at 00:36
  • 26
    Doesn't explain how they are different. All these definitions seem rather equivalent. – hasen Dec 06 '17 at 04:22
  • 3
    There is no need to get into VMs for green threads. – Deduplicator Jun 10 '19 at 15:47
  • Why current JVM threads are not green? – gstackoverflow Jun 28 '19 at 14:10
  • 1
    @gstackoverflow: Java threads are OS threads. The JVM schedules them through the OS. – Robert Harvey Jun 28 '19 at 14:26