Forgot your password?  

Not What You Meant?  There are 15 definitions for Yield.  Also try: Routine.

Coroutine | Research & Encyclopedia Articles

Print-Friendly   Order the PDF version   Order the RTF version
About 1 pages (431 words)
Coroutine Summary

 


Coroutine

Coroutines are collections of processes (which execute one at a time) that have the ability to transfer control to (or resume) each other. When control is transferred back to it, each coroutine resumes at the point where it last ended. When a coroutine transfers control, its data value is retained. The retention of data makes coroutines different from subroutines.

A subroutine is called and then upon completion returns; a coroutine resumes. When a subroutine returns, control is transferred to the instruction following the subroutine call and any data in the subroutine is lost. On the next call, the subroutine starts from the beginning and new data is created. Coroutines, however, can call another coroutine and resume at the point immediately following the call and preserve its data.

Coroutines interact in the following way:

  • One coroutine has control until it resumes another coroutine.
  • A coroutine is activated the first time it is resumed by another coroutine.
  • An active coroutine gives up control when it resumes another coroutine, but it retains its data. When it, in turn, is resumed, it continues from the point at which it last gave up control. A "good" coroutine runs to completion. If it is not resumed by some other coroutine, it cannot finish.

A related term is semi-coroutine, which is a hybrid of subroutines and coroutines. Like subroutines semi-coroutines must give control back to caller, but like coroutines their data and control state are preserved between calls.

Uses and Benefits of Coroutines

Coroutines are used in operating systems, compilers, discrete-event simulation programs, text manipulation, artificial intelligence, sorting, and numerical analysis programs. These applications can require several activities to be executing at the same time and values to be transferred between processes. Coroutines can be a mechanism for transferring values between a source and target and are useful as a means of modeling concurrent activities.

For example, coroutines can be used in the discrete simulation of a card game. Coroutine A selects a card. A then transfers control to coroutine B. B selects a card, then transfers control to coroutine C. C selects a card, then transfers control to, or resumes, A. The value of the cards in A is retained from its last selection and it picks up where it left off. This continues until a winner is declared.

A benefit of coroutines is their efficiency. Coroutines can be more efficient than subroutines because an activation record is not created at every call. Additionally, because coroutines can be resumed at the appropriate point to continue computations, their use can simplify the implementation of some algorithms. They also help to break large programs into smaller, more manageable pieces.

This is the complete article, containing 431 words (approx. 1 page at 300 words per page).

Ask any question on Coroutine and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
Coroutine from World of Computer Science. ©2005-2006 Thomson Gale, a part of the Thomson Corporation. All rights reserved.

Join BookRagslearn moreJoin BookRags

Join BookRagslearn moreJoin BookRags