Coroutines - Coroutines and Generators Coroutines and...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Coroutines and Generators Beta Draft - Do not distribute © 2001, By Randall Hyde Page 1329 Cor outines and Generators Chapter Three 3.1 Chapter Overview This chapter discusses tw o special types of program units kno wn as coroutines and generators. A coroutine is similar to a procedure and a generator is similar to a function. The principle dif ference between these program units and procedures/functions is that the call/return mechanism is dif ferent. Coroutines are especially useful for multiplayer g ames and other program fl o w where sections of code "tak e turns" e x ecut - ing. Generators, as their name implies, are useful for generating a sequence of v alues; in man y respects generators are quite similar to HLA s iterators without all the restrictions of the iterators (i.e., you can only use iterators within a FOREA CH loop). 3.2 Coroutines A common programming paradigm is for tw o sections of code to sw ap control of the CPU back and forth while e x ecuting. There are tw o w ays to achie v e this: preempti v e and cooperati v e. In a preempti v e sys - tem, tw o or more pr ocesses or thr eads tak e turns e x ecuting with the task switc h occurring independently of the e x ecuting code. A later v olume in this te xt will consider preempti v e multitasking, where the Operating System tak es responsibility for interrupting one task and transferring control to some other task. In this chapter we’ ll tak e a look at coroutines that e xplicitly transfer control to another section of the code 1 When discussing coroutines, it is instructi v e to re vie w ho w HLA s iterators w ork, since there is a strong correspondence between iterators and coroutines. Lik e iterators, there are four types of entries and returns associated with a coroutine: Initial entry. During the initial entry, the coroutine’s caller sets up the stack and otherwise ini- tializes the coroutine. Cocall to another coroutine / coreturn to the previous coroutine. Coreturn from another coroutine / cocall to the current coroutine. Final return from the coroutine (future calls require reinitialization). A cocall operation transfers control between tw o coroutines. A cocall is ef fecti v ely a call and a return instruction all rolled into one operation. From the point of vie w of the process e x ecuting the cocall, the cocall operation is equi v alent to a procedure call; from the point of vie w of the processing being called, the cocall operation is equi v alent to a return operation. When the second process cocalls the fi rst, control resumes not at the be ginning of the fi r st pr ocess , b ut immediately after the last cocall operation from that coroutine (this is similar to returning from a FOREA CH loop after a yield operation). If tw o processes e x e - cute a sequence of mutual cocalls, control will transfer between the tw o processes in the follo wing f ashion: 1. The term "cooperative" in this chapter doesn’t not imply the use of that oxymoronic term "cooperative multitasking" that
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/08/2011 for the course CS 101 taught by Professor Jitenderkumarchhabra during the Summer '11 term at National Institute of Technology, Calicut.

Page1 / 12

Coroutines - Coroutines and Generators Coroutines and...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online