{"id":1961,"date":"2023-12-06T12:20:56","date_gmt":"2023-12-06T11:20:56","guid":{"rendered":"https:\/\/oopm.org\/?page_id=1961"},"modified":"2024-12-15T10:16:19","modified_gmt":"2024-12-15T09:16:19","slug":"51-cooperative-coroutines","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=1961","title":{"rendered":"15. Cooperative coroutines"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages1961&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages1961&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">In this chapter, we introduce&nbsp;<em>coroutines<\/em>, which&nbsp;represent activities that may be suspended and later resumed at the point of suspension. Suspension may take place at several points during the activity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In its simplest form a coroutine is like a method object where execution may be temporarily suspended and later resumed.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In its most general form, it is like a parallel object in the sense that a coroutine object may execute methods and thus constitute an invocation stack (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">stack<\/mark>) as described in section <script>mkRef(\"The program execution\")<\/script>. The method object currently being executed may suspend the whole coroutine object including the stack. The difference is that two or more parallel objects may execute in parallel whereas at most one coroutine object at a time may be executed. This has the advantage that two or more coroutine objects cannot simultaneously access the same data as is the case for parallel objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The scheduling of coroutine objects is called&nbsp;<em>cooperative scheduling&nbsp;<\/em>since the programmer has to be  explicit about when\/where a coroutine object suspends its execution and about which coroutine object to execute\/resume next.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a later chapter, we introduce&nbsp;<em>preemptive coroutines,<\/em>&nbsp;where execution may be preempted. These coroutine objects are even closer to process objects. The coroutines described in this chapter is often called&nbsp;<em>cooperative coroutines.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Cooperative coroutines can be used for a number of useful algorithms, which we will give examples of later in this chapter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A coroutine object may suspend execution by executing a <code>suspend<\/code> statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em><em>aCoroutineObj<\/em><\/em>.suspend<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">where <code><em>aCoroutineObj<\/em><\/code> must be an expression that evaluates to a reference to the coroutine object that executes the statement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Execution may later be resumed by execution of a <code>resume<\/code> statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>resume(aCoroutineObj<\/em>)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> where <code><em>aCoroutineObj<\/em><\/code> must be an expression that evaluates to a reference to a coroutine object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A sketch of using <code>suspend<\/code> and <code>resume<\/code> is shown in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>      coroutineSketch: <strong>obj<\/strong>\n         <strong>class<\/strong> CoroutineEx: \n            ...\nL2:         this(CoroutineEx).suspend\nL5:         stmt \n            ...\n         aCoroutineObj: <strong>ref<\/strong> CoroutineEx\nL1:      aCoroutineObj := CoroutineEx\nL3:      ...  \nL4:      resume(aCoroutineObj)\n         ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We have inserted labels <code>L1<\/code>&#8211;<code>L5<\/code> before some of statements in the above code to be able to refer to these in the text below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The object <code>coroutineSketch<\/code> has the attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Class <code>CoroutineEx<\/code> describes objects that include a <code>suspend<\/code> statement.<\/li>\n\n\n\n<li>A variable reference <code>aCoroutineObj<\/code> of type <code>CoroutineEx<\/code>.<\/li>\n\n\n\n<li>An instance of <code>CoroutineEx<\/code> is generated and assigned to <code>aCoroutineEx<\/code> at the label L1.<\/li>\n\n\n\n<li>As part of the generation of this object, its items are executed.<\/li>\n\n\n\n<li>When execution of the <code>CoroutineEx<\/code> object arrives at L2, its execution is suspended.<\/li>\n\n\n\n<li>Control then returns to <code>coroutineSketch<\/code>, which is the <em>invoker<\/em> of <code>CoroutineEx<\/code>, and execution continues at L3.<\/li>\n\n\n\n<li>At L4, execution of <code>aCoroutineObj<\/code> is resumed by execution of the <code>resume<\/code> statement <code>resume(aCoroutineObj)<\/code>.<\/li>\n\n\n\n<li>This implies that execution of <code>aCoroutineObj<\/code> is resumed at <code>stmt<\/code> L5<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When a coroutine has no more statements to execute, it <em>terminates<\/em> execution in the same way as a method object or material object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the next section, we will show concrete  examples of using coroutines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=1961\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>In this chapter, we introduce&nbsp;coroutines, which&nbsp;represent activities that may be suspended and later resumed at the point of suspension. Suspension may take place at several points during the activity. In its simplest form a coroutine is like a method object where execution may be temporarily suspended and later resumed.&nbsp; In its most general form, it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":21,"menu_order":15,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1961","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1961"}],"version-history":[{"count":40,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1961\/revisions"}],"predecessor-version":[{"id":10440,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1961\/revisions\/10440"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/21"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}