Continuations and threads (was Re: Iterators & generators)

Paul Prescod paul at prescod.net
Thu Feb 17 20:57:02 EST 2000


Aahz Maruch wrote:
> 
> Assuming that I've understood everything you've said about
> continuations, I think I also understand what Tim meant about modeling
> threads with continuations and vice-versa.  Seems to me that one could
> easily implement continuations with a process that forks off and blocks
> waiting for one of two signals: resume or die.  (When a process forks,
> both processes have almost exactly the same state, right?)

Okay, but how is implementing threading via fork() using continuations?
The underlying OS task switcher is doing all of the work, not the
language's continuation feature.

> If everything I've said above is correct, then it should be reasonably
> straightforward to also model continuations with threads (but probably
> not Threading.py).  I leave the reverse model as an exercise for the
> reader.  ;-)

This half would probably work if your threads work like fork(). If they
work like: "StartThisFunctionInAnotherThread( func )" then it won't
work.

> Am I right?

Well your version had some improvements but I think that they make more
clear the fact that the while loop is not useful. The continuations will
bounce back and forth forever by themselves. In this case they are very
much like "goto" in basic which probably explains why Common Lisp people
hate them.

As has been pointed out, it is pretty tricky to come up with simple,
compelling examples. Programming languages tend to evolve the flow
control constructs that people need rather than requiring them to
implement flow control themselves! If we could only go back to before
the invention of while, for, try/except and so forth, we could all learn
to do everything with continuations and forget about all of that
syntactic sugar. :)

-- 
 Paul Prescod  - ISOGEN Consulting Engineer speaking for himself
"The calculus and the rich body of mathematical analysis to which it
gave rise made modern science possible, but it was the algorithm that
made possible the modern world." 
        - from "Advent of the Algorithm" David Berlinski
	http://www.opengroup.com/mabooks/015/0151003386.shtml




More information about the Python-list mailing list