lua-users home
lua-l archive

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


> Does the manual tell you how to use co-routines from c++, or multiple
> states?

Yes, actually. Look at section 3.20. The 3rd chapter contains all the
information on interfacing with Lua from C/C++. I'd suggest reading that
chapter when you get a chance. Understanding that chapter may also help.

And I quote:

To manipulate threads as coroutines, Lua offers the following functions:

       int lua_resume (lua_State *L, int narg);
       int lua_yield (lua_State *L, int nresults);
To start a coroutine, you first create a new thread; then you push on
its stack the body function plus any eventual arguments; then you call
lua_resume, with narg being the number of arguments. This call returns
when the coroutine suspends or finishes its execution. When it returns,
the stack contains all values passed to lua_yield, or all values
returned by the body function. lua_resume returns 0 if there is no
errors running the coroutine, or an error code (see 3.15). In case of
errors, the stack contains only the error message. To restart a
coroutine, you put on its stack only the values to be passed as results
from yield, and then call lua_resume. 

The lua_yield function can only be called as the return expression of a
C function, as follows:

       return lua_yield (L, nresults);
When a C function calls lua_yield in that way, the running coroutine
suspends its execution, and the call to lua_resume that started this
coroutine returns. The parameter nresults is the number of values from
the stack that are passed as results to lua_resume.