@darkness the whole point is to avoid threads. Probably it can be done the way u say, but it will be messy, prown to bug etc.
Anyway in a nutshell i will explain i will gloss over some of the details. But i will try and explain how continuation work.
simplest place to start is with python generators
>>> def foo() :
... yield 1
... yield 2
... yield 3
...
>>> foo
<function foo at 0x84d5f44>
a=foo()
a
<generator object at 0x84dd2cc>
a.next()
1
a.next()
2
you have generator called foo
when i construct it and assign it to a
when i call a.next() it gives 1 , 2 etc
what is happening every time i next it runs it till next yield, and return that value in yield. when it hits yield it saves current state.
i will now show how done in scheme (u would not use this in real life, just shown like this for demo purpose)
(define cc '()) <--- cc is the state
(define (foo)
(let ((i 0))
(call/cc
(lambda (k) <---- k is the continuation
(set! cc k))) <--- save value of k in cc
(set! i (+ i 7))
i))
I load the above code into scheme
foo it the function everytime it start it take i and adds 7 to it. but i is local inside the function.
#;2> cc
() <--- cc has no value () is nil
#;3> (foo)
7 call foo 7 is returned
#;4> cc
#<procedure (a7138 . results798)> <-- notice value of cc now is a procedure this is the continuation
#;5> (cc '()) <--- call cc with a null arguement (using macros i can get arround this, but want to keep simple, a continuation always takes 1 arg)
14
#;6> (cc '())
21
#;7> (cc '())
28
#;8> (cc '())
35
and it counts
using a macro i can make the code look exactly python (well with brackets ofcourse

)
but it go deeper then that.
I can pass the continuation back and forth etc.
in my case i am using it to emulate a statemachine.
so u will have a statemachine, but it will appear to be linear.