Continuations and threads (was Re: Iterators & generators)

Moshe Zadka moshez at math.huji.ac.il
Fri Feb 18 06:18:51 CET 2000


On 17 Feb 2000, Samuel A. Falvo II wrote:

> >Oh, continuations *cannot* be simulated by threads. (Coroutines can be
> 
> OK, that's informative.  So much for the differences between threads and
> continuations.  But I'm still left wondering just what the heck
> continuations are.  :)

Well, just like the rest of us. I'll explain continuations in Scheme,
because that's the only continuatios I know of. I assume Christian will
pop up with Stackless's module "continuation" any time now.

Scheme has a function called call-with-current-continuation (usually
aliased in implementation to call/cc). It takes one function, which takes
one parameter, and calls it with an opaque object -- the continuation. The
continuation is a function accepting one parameter (OK, I'm simplifying
the truth here, but never mind). When the continuation is called with a
parameter "x", the function which called call/cc thinks call/cc returned
with the value "x". On the other hand, if the function which was given to
call/cc returns with the value "x", the function which called call/cc also 
thinks call/cc returned with value "x". 

You can use it like setjmp/longjmp, in things like callbacks: before
before calling the callback, set a global variable to the current
continuation, and call it. Any time the callback wants to "longjmp", it
merely calls the continuation with a pre-determined value. When the
call/cc returns that value, you know the callback jumped.

That was probably very incomprehensible, but that's quite all right.
Continuations are hard. The timbot explained to me a few times, so by now
I can fool lots of people into believing I understand them, but it still
sometimes baffles me.


--
Moshe Zadka <mzadka at geocities.com>. 
INTERNET: Learn what you know.
Share what you don't.





More information about the Python-list mailing list