How do coroutines differ from threads?
Short answer
Threads are OS-scheduled, run truly in parallel (on multiple cores), and each has its own stack with relatively high memory and context-switch cost.
Advertisement
AModel answer
Threads are OS-scheduled, run truly in parallel (on multiple cores), and each has its own stack with relatively high memory and context-switch cost. Coroutines are cooperatively scheduled within a thread—they suspend/resume at explicit points, are extremely lightweight, and don't provide parallelism by themselves. Coroutines handle concurrency (many tasks) efficiently; threads provide parallelism.
Real-world example
One thread runs thousands of coroutines cooperatively, versus the high cost of thousands of OS threads.
Likely follow-ups
- Do coroutines run in parallel by themselves? | Which is more lightweight?
Related topics