Created
August 19, 2014 20:31
-
-
Save Mzk-Levi/bf70cf4737bb37508973 to your computer and use it in GitHub Desktop.
Coroutines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Coroutines are computer program components that generalize subroutines to allow multiple entry points for suspending and resuming execution at certain locations. | |
"Subroutines are special cases of ... coroutines." –Donald Knuth. | |
When subroutines are invoked, execution begins at the start, and once a subroutine exits, it is finished; | |
an instance of a subroutine only returns once, and does not hold state between invocations. | |
By contrast, coroutines can exit by calling other coroutines, | |
which may later return to the point where they were invoked in the original coroutine; | |
from the coroutine's point of view, it is not exiting but calling another coroutine. | |
Thus, a coroutine instance holds state, and varies between invocations; | |
there can be multiple instances of a given coroutine at once. | |
The difference between calling another coroutine by means of "yielding" to it | |
and simply calling another routine (which then, also, would return to the original point), | |
is that the latter is entered in the same continuous manner as the former. | |
The relation between two coroutines which yield to each other is not that of caller-callee, but instead symmetric. | |
-- Wikipedia |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment