Subroutines vs Coroutines vs Generators
Coroutines vs Subroutines
Subroutines:
- invoked once and executes until it completed.
- can be translated to a coroutine which does not call
yield.
Coroutines:
- a more generalized form of subroutines.
- can be entered, exited, and resumed at many different points.
- can pause execution and yield control back to the caller or another coroutine. The caller can then resume the coroutine when appropriate. (i.e. it may suspend its execution in one thread and resume in another one. A coroutine is not bound to any particular thread.)
- used for cooperative multitasking and are often compared to fibers, lightweight threads and green threads
Coroutines vs Generators
Generators: a.k.a. "semicoroutines": a more limited form of coroutine that may only yield to its caller. Primarily used to simplify the writing of iterators.
Both can yield multiple times, suspending their execution and allowing re-entry at multiple entry points.
Key difference:
- Generators: can only yield to its caller. Cannot specify a coroutine to jump to in the
yieldstatement. - Coroutines: can control where execution continues immediately after they yield.
Coroutines vs Threads
- threads: scheduled by the operating systems.
- coroutines: scheduled by the users. (A.k.a. lightweight threads or green threads.)
Coroutines in Languages
- C++: coroutines introduced in C++20.
- Python:
- https://www.python.org/dev/peps/pep-0492/
asyncandawait
- Kotlin: in an extension lib
kotlinx.coroutines. (NO keywords and NOT in the standard library)launch,delay,suspend fun
- Java: Project Loom.
Coroutines vs goroutines
- goroutines imply parallelism; coroutines in general do not.
- goroutines communicate via channels; coroutines communicate via yield and resume operations.
When to use Coroutines
- data structure iterators.
- event-driven code without the inversion of control.
- cooperative multitasking.
- concurrency frameworks such as actors, async-await and dataflow networks.
- expressing asynchrony, and better composition of asynchronous systems.
- capturing continuations.
- expressing backtracking algorithms.
- AI agents such as behavior trees.
Recommended ReadingSponsored
Versus
- 32-bit vs 64-bit
- AES vs RSA
- Agent vs Agentless
- Annotations vs Decorators
- Terraform vs Ansible
- BASH vs ZSH
- BigQuery vs Bigtable
- Block Storage vs File Storage vs Object Storage
- Canvas vs SVG
- cgroupfs vs systemd
- Constructor vs Init() vs Factory
- Container Image vs Helm Chart vs OCI Bundle
- Containers vs Virtual Machines (VMs)
- C vs C++
- Java vs C++
- Rust vs C++
- crictl vs ctr
- Dagger vs Guice
- DMA vs RDMA
- DOM vs Virtual DOM vs Shadow DOM
- DQL vs DDL vs DCL vs DML
- eBPF vs Sidecar
- Flux vs Redux
- Full Disk Encryption vs Partition Encryption
- Gatekeeper vs OPA
- Google vs Open Source
- Why MCP uses JSON-RPC 2.0 instead of grpc
- gRPC vs OpenAPI
- gtag.js vs Google Tag Manager (GTM)
- Versus
- Kotlin vs Java
- Jetty vs Netty
- .js vs .jsx vs .ts vs .tsx
- Kata Containers vs KubeVirt
- Linux Namespace vs Kubernetes Namespace
- Linux vs BSD
- LLVM vs JVM
- Data Mining vs Machine Learning vs Artificial Intelligence vs Data Science
- Managed Kubernetes vs Self-managed Kubernetes
- Management Plane vs Control Plane vs Data Plane
- Microcontroller vs Microprocessor vs Computer
- MMU vs IOMMU
- Single-tenant vs Multi-tenant vs Multi-single-tenant
- Multithreading vs Multiprocessing vs Distributed Systems
- Node.js vs Erlang
- OCI Image vs Docker Image
- OCI vs CRI
- OpenSSH vs OpenSSL
- Pass-by-value vs Pass-by-reference
- Podman vs nerdctl
- POSIX vs SUS vs LSB
- Private Key vs Public Key vs Certificate
- Proto Edition 2024 vs Proto Edition 2023
- Proto2 vs Proto3 vs Proto Editions
- Message Queue vs PubSub
- React vs Flutter vs Angular
- REST vs SOAP
- RSA vs ECDSA
- select vs. poll vs. epoll
- SLI vs SLO vs SLA
- Spanner vs Bigtable
- Static Site Generation vs Server-side Rendering vs Client-side Rendering
- Stack based VM vs Register based VM
- Stateless vs Stateful
- Strong Consistency vs Eventual Consistency
- stubs vs mocks vs fakes
- Subroutines vs Coroutines vs Generators
- Software Engineer vs Site Reliability Engineer
- Symlinks vs Hard Links
- JAX vs Tensorflow
- Tensorflow vs PyTorch
- Terminal vs Shell
- Throttling vs Load Shedding
- Unix Domain Sockets vs Network Sockets
- Unix Domain Sockets vs Shared Memory
- URI vs URN vs URL vs URC
- Valkey vs Redis vs Memcached
- Vi vs Vim vs gVim vs Neovim
- WAL vs rollback journal

