COROUTINES - the C co-routine package.

Description:

Co-routines are of particular use in file processing and simulation applications. On many systems, co-routines are called threads.

In file processing, co-routines let you separate records or characters in time, rather than in space (i.e. physical file position), and view your program in terms of data flow from module to module, rather than flow of control.

In simulation, co-routines allow a more natural modelling of a set of co-operating processes. It should be stressed that although co-routines share a number of properties with asynchronous processes (preservation of local variables etc.), which make modelling easy, co-routines are not separate processes, and you must still manage flow of control between them.

For a formal definition of a co-routine and a full explanation of the fundamental concepts, see the technical literature (Knuth, CACM etc.).

Preserving State:

The current state of execution of a function can be largely described by its program instruction counter (IC) and its stack frame and pointer. The IC gives the location where execution is taking place, and the stack frame provides the values of all arguments and other storage local to the function. If the IC, stack frame, and stack pointer are preserved when a function is suspended, the function may be resumed at the exact point of suspension by restoring these values.

In regular functions, the current state of the function is lost when the function returns; the only way the state may be preserved when transferring to another function is to call the other function as a subroutine of the first. Then, of course, the state of the subroutine must be lost when it returns to resume execution in its caller.

In a like manner, one can conceive of a group of functions, like those that constitute a program, which can only preserve the group state by calling other groups of functions (programs) as subprograms. The state of a subprogram, as with the state of a called function, vanishes when the subprogram returns to its caller. The states of both the caller and callee cannot be easily preserved.

Co-routines, on the other hand, are groups of functions which have been given a mechanism for preserving their states before transferring to other co-routines. Transfers among co-routines do not involve the regular caller/callee hierarchy, and the states of all co-routines may be thought of as existing concurrently.

See Also:

expl nsc coro implementation

expl nsc coro summary

expl nsc coro header

expl nsc coro main

Copyright © 1996, Bull HN and Thinkage Ltd.