84

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.

0

1 Answer 1

141

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.

9
  • 21
    I can only say for myself - this answer is very valuable to me. I share opinion with Robert on this.
    – DejanLekic
    Commented Aug 22, 2014 at 15:16
  • 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
    Commented Aug 23, 2014 at 0:36
  • 27
    Doesn't explain how they are different. All these definitions seem rather equivalent.
    – hasen
    Commented Dec 6, 2017 at 4:22
  • 4
    There is no need to get into VMs for green threads. Commented Jun 10, 2019 at 15:47
  • 1
    @gstackoverflow: Java threads are OS threads. The JVM schedules them through the OS. Commented Jun 28, 2019 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.