What is a coroutine? How are they related to concurrency?
I find an explanation from this link is pretty straight forward. None of those answers try to explain concurrency vs parallelism except the last bullet point in this answer.
cited from "programming Erlang", by Joe Armstrong, the legendary:
a concurrent program can run potentially faster on a parallel computer.
a concurrent program is a program written in a concurrent programming language. We write concurrent programs for reasons of performance, scalability, or fault tolerance.
a concurrent programming language is a language that has explicit language constructs for writing concurrent programs. These constructs are an integral part of programming language and behave the same way on all operating systems.
a parallel computer is a computer that has several processing units (CPUs or cores) that can run at the same time.
So concurrency is not the same as parallelism. You can still write concurrent programs on a single-core computer. The time-sharing scheduler will make you feel your program is running concurrently.
The concurrent program has the potential to run in parallel in a parallel computer but not guaranteed. OS may only give you one core to run your program.
Therefore, concurrency is a software model from a concurrent program that doesn't mean your program can run in parallel physically.
The word “coroutine” is composed of two words: “co” (cooperative) and “routines” (functions).
a. does it achieve concurrency or parallelism?
To be simple, let's discuss it on a single-core computer.
Concurrency is achieved by time-shares from OS. A thread executes its code in its assigned time frames on the CPU core. It can be preempted by OS. It may also yield control to OS.
A coroutine, on the other hand, yields control to another coroutine within the thread, not to OS. So all coroutines within a thread still exploit the time frame for that thread without yielding the CPU core to other threads managed by OS.
Therefore, you can think of coroutine achieves time-shares by the user not by OS (or quasi-parallelism). Coroutines run on the same core assigned to the thread that runs those coroutines.
Does Coroutine achieve parallelism? If it's CPU-bound code, no. Like time-shares, it makes you feel they run in parallel but their executions are interleaved not overlapped. If it's IO-bound, yes, it achieves parallel by hardware (IO devices) not by your code.
b. the difference with function call?
As the pic shows, it doesn't need to call return
to switch control. It can yield without return
. A coroutine saves and shares state on the current function frame (stack). So it's much more lightweight than function since you don't have to save registers and local variables to stack and rewind call stack when call ret
.
From Python Coroutine:
Execution of Python coroutines can be suspended and resumed at many points (see coroutine). Inside the body of a coroutine function, await and async identifiers become reserved keywords; await expressions, async for and async with can only be used in coroutine function bodies.
From Coroutines (C++20)
A coroutine is a function that can suspend execution to be resumed later. Coroutines are stackless: they suspend execution by returning to the caller. This allows for sequential code that executes asynchronously (e.g. to handle non-blocking I/O without explicit callbacks), and also supports algorithms on lazy-computed infinite sequences and other uses.
Compare with other's answer:
In my opinion, the resumed later part is a core difference, just like @Twinkle's.
Although many fields of the document are still work in progress, however, this part is similar to most answer, except @Nan Xiao 's
Coroutines, on the other hand, are collaborative: at any given time, a program with coroutines is running only one of its coroutines, and this running coroutine suspends its execution only when it explicitly requests to be suspended.
Since it's quoted from Program in Lua, maybe it's language related(not familiar with Lua currently), not all document mentioned the only one part.
The relation with concurrent:
There is an "Execution" part of the Coroutines (C++20).Too long to quote here.
Besides the detail, there are several states.
When a coroutine begins execution
When a coroutine reaches a suspension point
When a coroutine reaches the co_return statement
If the coroutine ends with an uncaught exception
When the coroutine state is destroyed either because it terminated via co_return or uncaught exception, or because it was destroyed via its handle
as the comment from @Adam Arold under @user217714's answer. It's concurrency.
But it's different from multithreading.
from std::thread
Threads allow multiple functions to execute concurrently. Threads begin execution immediately upon construction of the associated thread object (pending any OS scheduling delays), starting at the top-level function provided as a constructor argument. The return value of the top-level function is ignored and if it terminates by throwing an exception, std::terminate is called. The top-level function may communicate its return value or an exception to the caller via std::promise or by modifying shared variables (which may require synchronization, see std::mutex and std::atomic)
Since it's concurrency, it works like multithreading especially when waiting is unavoidable(from the OS perspective), that's also why it's confusing.
From Programming in Lua, "Coroutines
" section:
A coroutine is similar to a thread (in the sense of multithreading): it is a line of execution, with its own stack, its own local variables, and its own instruction pointer; but it shares global variables and mostly anything else with other coroutines. The main difference between threads and coroutines is that, conceptually (or literally, in a multiprocessor machine), a program with threads runs several threads in parallel. Coroutines, on the other hand, are collaborative: at any given time, a program with coroutines is running only one of its coroutines, and this running coroutine suspends its execution only when it explicitly requests to be suspended.
So the point is: Coroutines are "collaborative". Even in multi-core system, there is only one coroutine running at any given time (but multiple threads can run in parallel). There is non-preemptive between coroutines, the running coroutine must relinquish the execution explicitly.
For "concurrency
", you can refer Rob Pike's slide:
Concurrency is the composition of independently executing computations.
So during coroutine A's execution, it passes control to coroutine B. Then after some time, the coroutine B passes control back to coroutine A. Since there is dependency between coroutines, and they must run in tandem, so the two coroutines are not concurrency.
Basically, there are two types of Coroutines:
Kotlin implements stackless coroutines — it’s mean that the coroutines don’t have own stack, so they don’t map on native thread.
These are the functions to start the coroutine:
launch{}
async{}
You can learn more from here :
https://www.kotlindevelopment.com/deep-dive-coroutines/
https://blog.mindorks.com/what-are-coroutines-in-kotlin-bf4fecd476e9