LearnViz CompSci
Home / Learn / Concurrency & Parallelism / Coroutines

Intermediate Topic 20 of 51 Concurrency & Parallelism

Coroutines

Functions that suspend and resume cooperatively.

How it works

A coroutine is a function that can suspend its execution and later resume where it left off, preserving local state, allowing cooperative multitasking within one thread. Unlike threads, coroutines are scheduled by the program rather than by the operating system, and switching is cheap, so thousands or millions can exist. They can be stackful, saving a whole call stack as in Lua and Go's goroutines, or stackless, as in Python generators-based async, C++20 and Kotlin, transformed into state machines. Generators are a special case yielding values.

Where it is used

Asynchronous I/O frameworks, game scripting of sequences over many frames, generators for lazy sequences and simulation of many concurrent activities use coroutines.

Common pitfall

Cooperative scheduling means a coroutine that never yields blocks the others. Debugging stack traces across suspension points is harder. Mixing blocking calls inside coroutines stalls the scheduler. Cancellation and exception propagation need structure, such as structured concurrency. Understand the scheduler, yield points and lifetime of captured data.

Where it fits in the course

Coroutines is topic 20 of 51 in Concurrency & Parallelism, rated Intermediate. It follows Event-Driven Concurrency. The next topic is Futures and Promises Model.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Concurrency & Parallelism quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Coroutines?
Functions that suspend and resume cooperatively. It belongs to Concurrency & Parallelism and is an intermediate-level topic.
Where do people use Coroutines?
Asynchronous I/O frameworks, game scripting of sequences over many frames, generators for lazy sequences and simulation of many concurrent activities use coroutines.
What is the most common mistake with Coroutines?
Cooperative scheduling means a coroutine that never yields blocks the others.
What should I learn before this topic?
Start with Event-Driven Concurrency, the topic just before it in Concurrency & Parallelism.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Concurrency & Parallelism quiz.
Is Coroutines hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Coroutines?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Coroutines?
Event-Driven Concurrency, Futures and Promises Model, Async/Await are closely connected. The full list is in the related topics section of this page.
What should I study after Coroutines?
Continue with Futures and Promises Model, the next topic in Concurrency & Parallelism.

Related topics