[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: That're not coroutines
- From: Edgar Toernig <froese@...>
- Date: Fri, 07 Jun 2002 21:48:32 +0200
Hi.
Sorry, but what's called coroutines in Lua 5.0 are no coroutines.
You may call it "microthreads" or something like that, but not
coroutines.
I'll quote from TAOCP by D. E. Knuth:
"Subroutines are special cases of more general program
components, called coroutines. In contrast to the un-
symmetric relationship between a main routine and a sub-
routine, there is complete symmetry between coroutines,
which call on each other."
The "coroutines" in Lua5 just implement regular subroutines
with preserved state. But the symmetry of "calling each other"
does not work. In fact, something like a "yield" is a
higher level concept that has nothing to do with basic
coroutines but seems to be the only way in Lua5's coroutines
to leave one.
I.e. this simple ping-pong example does not work:
a = coroutine.create(function()
for i=1,5 do
print"a"
b()
end
-- hmm... no coroutine.main to go to.
end)
b = coroutine.create(function()
for i=1,5 do
print"b"
a()
end
-- hmm... no coroutine.main to go to.
end)
a()
print"end"
It gives:
a
b
thread is active - cannot be resumed
stack traceback:
1- [C]: in function `a'
2- t.lua:14: in function <t.lua:11>
stack traceback:
1- [C]: in function `b'
2- t.lua:6: in function <t.lua:3>
stack traceback:
1- [C]: in function `a'
2- t.lua:19: in main chunk
Ciao, ET.
PS: Just MHO, but I don't like all this ugly craft in ldo.c to
get these strange Lua-only microthreads just to avoid ~10 lines
of system dependent code...