lua-users home
lua-l archive

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

Hi there,

Within the add function, you would call lua_yield, and return it's return value.

later you would use lua_resume to resume the thread.

as for what to pass into lua_yield and lua_resume, I'd suggest you read the lua programming reference for those functions

hope that helps.


On Tue, Jul 8, 2008 at 10:20 AM, Brandon <> wrote:
I've searched the lua mailing list for "pausing lua code", and came
across several posts where somebody asked exactly what I want to do. It
seems it might be possilbe, it might not, the answers conflict. Some
people say that a patch is required, but I would prefer to avoid that.

I would like the C code be able to pause the execution of the lua
script, and then resume at a later time. I've looked into the
lua_yield() and lua_resume() functions, and they seem to be what I
want, but I can't get them to work like I thought they would.

Here is some example code:

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

lua_State * L;

int add(lua_State * L)
       int num_args = lua_gettop(L);
       int i, sum = 0;

       for (i = 1; i <= num_args; i++) {
               sum += lua_tonumber(L, i);

/* yield here */
/* return ??? */

void cont()
       /* What goes here to continue script execution? */

int main(int argc, char ** argv)
       L = lua_open();
       lua_register(L, "add", add);

       luaL_dostring(L, "print(add(3, 4, 5))");

       // Do stuff here after script execution is paused in add

       cont(); // continue script execution

       return 0;

What goes in the blanks? I can get the script to pause by adding one of
various incantations of lua_yield to the "add" function, such as
lua_yield(L, x), where x is anything. But I can't get it to resume
again. I've tried several variations of lua_resume(L, x) in cont(), but
it doesn't seem to have any effect.

I have a couple of ideas to get around this problem. I could execute
the lua script in another thread, have the C function pause that
thread, and then restart the thread when the host is ready, and the
function can finally return. But I'd rather not add threads to my
project. Or, I could execute the lua script one line at a time, and
before executing the pausing function ("add" in the example), I can
execute the other stuff first, and then continue the lua script,
knowing that the data is ready for it. It seems that sending lua one
line at a time would degrade performance though, and it would be
somewhat error prone. My current solution involves setting a
waiting_function variable in the "add" function, and then running a
sleep/(execute other stuff)/(check a global variable) loop. When the
rest of the code is ready to continue, that other code sets the global
variable to the needed data, and then the add function returns. This is
clunky, and requires that my lua interface code is aware of the rest of
the project, whereas I would prefer it remain simple and ignorant.

I hope somebody out there knows how to do this.