[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: What's the difference between a "coroutine" and a "thread" in Lua?
- From: "Gregg Reynolds" <dev@...>
- Date: Mon, 13 Aug 2007 10:58:44 -0500
On 8/13/07, David Kastrup <dak@gnu.org> wrote:
>
> This would hold at best on single processors: on multiple processors,
> several threads can run simultaneously unpreempted. However,
> coroutines, being strictly synchronized, can have something that
> threads in general don't: they can pass values. Yielding in a Lua
> thread will both pass and accept a value to and from the controlling
> Lua thread. This is obviously only possible when the caller is
> suspended until the called thread yields. Apart from the switching of
> the stacks, this is completely identical to function calls.
>
Another thing: the surface syntax of Lua makes the relation of
coroutines appear to be asymmetric. But I think coroutines are
symmetric; yield and resume are synonyms meaning "invoke
continuation". Just for fun, using mock assembler:
// global vars to hold jump addresses:
ADDR resume, cc
INT result
...
mov 1 into result
mov continuation-x into resume
mov continuation-a into cc // current-continuation
jmp resume
continuation-a: // coroutine a
...do sth with result...
mov 2 into result
mov cc into resume
mov continuation-b into cc
jmp resume
continuation-b:
...do sth with result
mov 3 into result
mov cc into resume
mov continuation-a into cc // reset
if <foo>
jmp resume // next jmp to sub-resume restarts coroutine
else
jmp cc // loop
continuation-x: // coroutine x
...do sth with result
mov 4 into result
mov cc into resume
mov continuation-y into cc
jmp resume
continuation-y:
...do sth with result
mov 5 into result
mov cc into resume
mov continuation-x into cc // reset
if <bar>
jmp resume // next jmp to sub-resume restarts coroutine
else
jmp cc // loop
I can't be certain it works since I just whipped it up, but I think
it's a pair of cooperating coroutines, that pass data without a
stack. With pseudo-continuations. In an infinite loop. How many
threads? Surely every continuation should be considered a thread, no?
;)