Skip to main content
Definition Website Computer and Technology

Coroutine

Subject: Computer and Technology noun /ˈkoʊ.ruːˌtiːn/ 1 definition

  1. Definition (noun)

    Entry #198 · Updated

    A coroutine is a generalized subroutine that can suspend its execution at designated points, yield control to another routine, and later resume from the exact position where it paused with its local state preserved. Unlike a standard subroutine—which executes from start to return in a single invocation with a fresh call frame—a coroutine supports multiple entry and exit points without terminating its execution context. This capability enables cooperative multitasking, stateful generators, and non-blocking I/O pipelines without requiring operating-system thread context switches.

    Common Types

    • Stackful Coroutines: Allocate a dedicated call stack for each coroutine, allowing suspension and resumption from arbitrarily nested function calls (such as Lua coroutines or fibers).

    • Stackless Coroutines: Store local state in a compiler-generated state machine frame rather than a full call stack, restricting direct suspension to the coroutine body itself (such as Python async/await or C++20 coroutines).

    • Asymmetric Coroutines: Maintain a caller-callee hierarchy where yielding always returns control to the routine that resumed the coroutine, making them well suited for generators and structured async tasks.

    • Symmetric Coroutines: Provide a single transfer primitive that passes control directly between peer coroutines by name, creating a flat, non-hierarchical control flow.

    Example

    When a backend handler executes await fetch_invoice(order_id), the coroutine suspends at the socket read, yields control back to the event loop to process other incoming requests, and resumes immediately after the await once the HTTP payload arrives. Because its local frame—including order_id and retry counters—stays intact while paused, the request finishes cleanly without blocking or spawning a separate OS thread.