[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: What's the difference between a "coroutine" and a "thread" in Lua?
- From: David Kastrup <dak@...>
- Date: Mon, 13 Aug 2007 18:23:21 +0200
"Gregg Reynolds" <email@example.com> writes:
> On 8/13/07, David Kastrup <firstname.lastname@example.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
Yes, this has occured to me as well: the language is not expressing
this symmetry. However, there are some slight differences:
Yielding is always done to the thread that resumed, while resuming can
be done to any thread. This is similar to function calls: they always
return to the caller, but calling can be done to any function.
So we are actually again in a caller/callee relationship. It is just
weaker than in function calling because the callee does not need to be
at function begin/exit.