{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/coroutine-concepts","result":{"data":{"markdownRemark":{"id":"7d13990e-1029-588d-9e4f-b60e3da60b32","html":"<p>コルーチンとスレッドの違いについて少し調べた内容を簡単にまとめました。</p>\n<h2 id=\"コルーチンとは\" style=\"position:relative;\"><a href=\"#%E3%82%B3%E3%83%AB%E3%83%BC%E3%83%81%E3%83%B3%E3%81%A8%E3%81%AF\" aria-label=\"コルーチンとは permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>コルーチンとは</h2>\n<p><a href=\"https://ja.wikipedia.org/wiki/%E3%82%B3%E3%83%AB%E3%83%BC%E3%83%81%E3%83%B3\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Wikipedia</a> によると、</p>\n<blockquote>\n<p>コルーチン（英: co-routine）とはプログラミングの構造の一種。サブルーチンがエントリーからリターンまでを一つの処理単位とするのに対し、コルーチンはいったん処理を中断した後、続きから処理を再開できる。</p>\n</blockquote>\n<p>とのことで、コルーチンは<strong>中断・再開を制御できるのが特徴</strong>の、並行処理の一種のようです。</p>\n<p>最近よく使われている言語では、C# (特に Unity の例が多い)、Swift、Kotlin などがサポートしています。</p>\n<h2 id=\"コルーチンとスレッドの関係\" style=\"position:relative;\"><a href=\"#%E3%82%B3%E3%83%AB%E3%83%BC%E3%83%81%E3%83%B3%E3%81%A8%E3%82%B9%E3%83%AC%E3%83%83%E3%83%89%E3%81%AE%E9%96%A2%E4%BF%82\" aria-label=\"コルーチンとスレッドの関係 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>コルーチンとスレッドの関係</h2>\n<p>Kotlin のドキュメントの「<a href=\"https://kotlinlang.org/docs/coroutines-basics.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Coroutines basics</a>」というページからコルーチンの説明を引用します。</p>\n<blockquote>\n<p>A coroutine is an instance of suspendable computation. It is conceptually similar to a thread, in the sense that it takes a block of code to run that works concurrently with the rest of the code. However, a coroutine is not bound to any particular thread. It may suspend its execution in one thread and resume in another one.</p>\n</blockquote>\n<p>DeepL で翻訳すると…</p>\n<blockquote>\n<p>コルーチンは、サスペンダブルコンピュテーションのインスタンスです。コルーチンは、他のコードと同時に動作するコードブロックを実行するという意味で、スレッドと概念的に似ています。しかし、コルーチンは特定のスレッドに縛られることはありません。あるスレッドで実行を中断し、別のスレッドで再開することができます。</p>\n</blockquote>\n<p>とのことです。</p>\n<p>コルーチン自体はスレッドとは異なる概念であり、いずれかのスレッドで実行されるものの、中断したタイミングからはそのスレッドで別の処理が実行されることになります。</p>\n<p>参考</p>\n<ul>\n<li><a href=\"https://kotlinlang.org/docs/async-programming.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Asynchronous programming techniques | Kotlin</a></li>\n<li><a href=\"https://www.rhoboro.com/2019/02/09/coroutine-abstract.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">コルーチンは怖くない</a></li>\n<li><a href=\"https://www.it-swarm-ja.com/ja/architecture/%e3%80%8c%e3%82%b3%e3%83%ab%e3%83%bc%e3%83%81%e3%83%b3%e3%80%8d%e3%81%a8%e3%80%8c%e3%82%b9%e3%83%ac%e3%83%83%e3%83%89%e3%80%8d%e3%81%ae%e9%81%95%e3%81%84%e3%81%af%ef%bc%9f/968681459/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">architecture — 「コルーチン」と「スレッド」の違いは？</a></li>\n</ul>\n<h2 id=\"go-のゴルーチンは？\" style=\"position:relative;\"><a href=\"#go-%E3%81%AE%E3%82%B4%E3%83%AB%E3%83%BC%E3%83%81%E3%83%B3%E3%81%AF%EF%BC%9F\" aria-label=\"go のゴルーチンは？ permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Go のゴルーチンは？</h2>\n<p>Go のゴルーチンは、コルーチンを意識しているものの、プログラマが中断・再開を制御することはできないようです。</p>\n<p><a href=\"https://go-tour-jp.appspot.com/concurrency/1\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">A Tour of Go</a> で書かれているように、「軽量なスレッド」のように理解するのがよさそうです。</p>\n<p>参考</p>\n<ul>\n<li><a href=\"https://note.crohaco.net/2019/golang-goroutine/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">[Golang] Goroutine を支える技術 - くろのて</a></li>\n<li><a href=\"https://journal.lampetty.net/entry/concurrency-in-go-goroutines\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Goにおける並行処理 - goroutine編 - oinume journal</a></li>\n</ul>","fields":{"slug":"/posts/coroutine-concepts","tagSlugs":["/tag/programming/"],"autoRecommendPosts":["command-collection-for-binary-files","image-file-format-basics","audio-video-streaming-data-basics","computer-graphics-first-step"]},"frontmatter":{"date":"2021-06-25T12:31:05.416Z","description":"コルーチンとスレッドの違いについて少し調べた内容を簡単にまとめました。","tags":["programming"],"title":"コルーチンとは？スレッドとの違いは？","socialImage":null,"recommendPosts":["https://www.kanzennirikaisita.com/posts/software-developer-books-index"]}}},"pageContext":{"slug":"/posts/coroutine-concepts"}},"staticQueryHashes":["251939775","3942705351","401334301"]}