Re: C++ Threads, what's the status quo?
On 2007-01-15, Lourens Veen <lourens@rainbowdesert.net> wrote:
I had never heard of continuations when you first brought them up, but
I've read a little about them now. I'm not sure I understand them. My
first impression was that Scheme's call-with-continuation is a sort
of goto for functional languages. I'm not sure what that would be
good for, but my experience with functional languages is limited to
playing with algorithms; I've never done the things that Scheme seems
to use call/cc for in a functional language.
Think of continuations as "throws" that may _return_ an infinite number of
times. [Invalid code follows]
my_exception from_f;
void f()
{
while(1) {
// do something. this throw would not do "stack unwinding" or call
// destructors because it is expected to return.
int x = throw my_exception(y);
// do something else with x
}
}
void g()
{
try {
f();
} catch(from_f) {
// use from_f data
}
}
void h()
{
// return a value to the point of throw. x in f() will have value i
// at each call. depending on what g() does, it is not guaranteed
// that the flow of control will ever return here again after calling
// from_f(i).
for(int i = 0; i++; )
from_f(i);
}
An obvious problem are types of expressions: what the exception throws
and what is the left-hand side of the (hypothetical) "throw expression".
Could be solved within the existing language by having the continuation
object/thrown exception implement operator().
You may want to read on coroutines - they are easily implementable by
continuations. Other interesting things include recursive iterator
functions (eg. binary tree traversal) that return a single value per call.
IMO, it's a nice and useful feature.
Hope this made it a bit clearer :)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]