#note [Coroutines for Go](https://research.swtch.com/coro) に出てくる説明で - subroutines : 同時に実行している関数は1つで、return した場合にはスタックを消費する - coroutine : 同時に実行している関数は1つで、call した際にスタックを別で作りスタック間を行ったり (resume)、戻ったり (yield) することができるという構造 ※並行で処理すること自体は subroutine と coroutine の違いではなくそれはただの並行プログラミングの話 ![image](https://gyazo.com/4bcd1807c18620af382fef4995ff6273/thumb/1000) ![image](https://gyazo.com/1bb4d4b39da907a492999549b9c0cca8/thumb/1000) > Coroutines provide concurrency without parallelism: when one coroutine is running, the one that resumed it or yielded to it is not. > Threads provide more power than coroutines, but with more cost. The additional power is parallelism, and the cost is the overhead of scheduling, including more expensive context switches and the need to add preemption in some form. Typically the operating system provides threads, and a thread switch takes a few microseconds. > Generators provide less power than coroutines, because only the top-most frame in the coroutine is allowed to yield. That frame is moved back and forth between an object and the call stack to suspend and resume it. とはいえ [新雑誌「n月刊ラムダノート」の『「コルーチン」とは何だったのか?』の草稿を公開します](https://mametter.hatenablog.com/entry/2019/03/27/211140) の中では、 Generator も非対象コルーチンと解釈してそうではある (まあコルーチンの一種と捉えること自体は誤りではないのはそうっぽい) 余談として、 Kotlin の `Sequence` も Generator ## 参考 - [新雑誌「n月刊ラムダノート」の『「コルーチン」とは何だったのか?』の草稿を公開します](https://mametter.hatenablog.com/entry/2019/03/27/211140) - [『「コルーチン」とは何だったのか?』の裏話的な何か、とPythonコルーチン](https://bonotake.hatenablog.com/entry/2019/03/28/032324) - [Coroutines for Go](https://research.swtch.com/coro)