lua-users home
lua-l archive

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

> -What does the magic value which coroutine.create() returns mean? It seems
> to be a pointer to a c function, which is always the same. How does lua know
> how to use this to restart a running coroutine? (Is this related to
> "upvalues" somehow?

You may think about the current implementation as

  function coroutine.create (...)
    local co = reallycreate(...)
    return function () coroutine.resume(co) end

That is, coroutine.create creates the coroutine and returns a function
that resumes the coroutine when called. (Yes, it uses upvalues so that
the inner function, written in C, can access the "co" value.)

We will change that for Lua 5.0 beta, and we hope things will become
clearer. coroutine.create will do the job of "reallycreate", and will
return the coroutine itself. A new function, coroutine.wrap, will do
the job of the current coroutine.create (wrapping the coroutine inside
a function).

> -Is there a way to get the "magic value" for the currently running
> coroutine?


> -How do I kill a couroutine which is *not* the currently running one?

You do not need to. If you never resume it again, it will never run
again. (Its resources are eventually garbage collected.)

> -When I call a coroutine, can I find out if it has "return"ed, or yield()ed?

No directly. But you can use some protocol: e.g. the coroutine returns nil,
but never yields nil. Maybe in the future we will add a "coroutine.status"
function that returns the status of a coroutine: initialized, resumed,

-- Roberto