Microthreads without Stackless?

Patrick Maupin pmaupin at speakeasy.net
Fri Sep 17 23:43:23 EDT 2004


> But that's actually just what coroutines ARE.  Bryan seems to want
> some kind of hybrid between actual coroutines and a call stack.  Which
> isn't necessarily bad.  And is probably something various
> languages--like Perl 6--do support.

All the coroutine packages I've ever used (e.g. built-in to Modula-2,
add-ons for C, stuff I've built myself in assembly language, etc.):

a) Give each coroutine its own stack; and
b) allow inter-couroutine calls to be made from anywhere on this stack

This paradigm is quite natural with legacy stack-oriented languages. 
If you were to return from a function (e.g. similar to a python
'yield') using regular 'C' you would have to copy the local variables
off the stack somewhere during the yield processing, which could add a
lot of overhead.  Instead of doing this, you _call_ a function to
perform a yield, and your local variables remain on your stack.  Once
this paradigm is in place, the ability to call the yield function from
within a sub-function is natural and free (except for the extra memory
for a stack).

If you have to be at the top level to effect a switch out of one
coroutine into another, you have to organize your state and variables
such that it's probably not too much extra work to go one more step
and dispense with coroutines altogether.

I don't know what the "official definition" of coroutines is, or even
who the keeper of that definition would be, but my working mental
model of (and prior experience with) coroutines in the real world
includes a stack for each coroutine.  I admit that Knuth's coroutine
examples don't admit a stack, but then, neither do his subroutine
examples!

Regards,
Pat



More information about the Python-list mailing list