lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 20-Aug-05, at 10:50 PM, Brent Arias wrote:

I think Rici
is saying, just set the coroutine to nil, and the "yielded" coroutine will
be destroyed and cleaned-up (even though it never terminated).

Exactly what I'm saying. A thread does not ever need to terminate, in fact; here's a classic paradigm:

function server(message)
  while true do
    message = yield(pcall(handler[message.verb], message))
  end
end

"Receive a message; handle it producing some result(s), wait for the next message, repeat forever"

By the way, this assumes that handler was defined something like this, to simplify error checking in the server loop.

do
  local meta = {}
  local function unknown_action(message)
    error("Unknown message `"..tostring(message.verb).."'")
  handler = setmetatable({}, meta)
end
function handler.GET(message) ... end
-- etc.


By the way, I am not using a muti-threaded environment - I am not using
system threads. So when I speak of the main "thread", its just a manner of
speaking (I don't know what else to call it).

Seems fine to me. A Lua thread is a "thread" in the sense that it is a separate "thread of execution" in the Lua context. It has its own stack and associated control data. But it is not independent of other threads in the same Lua context. Lua itself calls the main thread the main thread, and who am I to argue? :

From lstate.h in Lua 5.1work6:

typedef struct global_State {
// <snip>
  struct lua_State *mainthread;
// <snip>
} global_State;