Lua合作例程

时间:2011-08-26 14:42:38

标签: c++ scripting lua coroutine

我正在尝试理解如何使用协同例程来“暂停”脚本并等待一些处理完成后再恢复。

也许我正在以错误的方式看待惯例。但我的尝试结构类似于此answer中给出的示例。

loop.lua中的循环永远不会达到第二次迭代,因此永远不会达到退出C代码中运行循环所需的i == 4条件。如果我不在loop.lua中产生,那么此代码将按预期执行。

的main.cpp

#include <lua/lua.hpp>

bool running = true;

int lua_finish(lua_State *) {
    running = false;
    printf("lua_finish called\n");
    return 0;
}
int lua_sleep(lua_State *L) {
    printf("lua_sleep called\n");
    return lua_yield(L,0);
}

int main() {
    lua_State* L = lua_open();
    luaL_openlibs(L);

    lua_register(L, "sleep", lua_sleep);
    lua_register(L, "finish", lua_finish);

    luaL_dofile(L, "scripts/init.lua");

    lua_State* cL = lua_newthread(L);
    luaL_dofile(cL, "scripts/loop.lua");

    while (running) {
        int status;
        status = lua_resume(cL,0);
        if (status == LUA_YIELD) {
            printf("loop yielding\n");
        } else {
            running=false; // you can't try to resume if it didn't yield
            // catch any errors below
            if (status == LUA_ERRRUN && lua_isstring(cL, -1)) {
                printf("isstring: %s\n", lua_tostring(cL, -1));
                lua_pop(cL, -1);
            }
        }
    }

    luaL_dofile(L, "scripts/end.lua");
    lua_close(L);
    return 0;
}

loop.lua

print("loop.lua")

local i = 0
while true do
    print("lua_loop iteration")
    sleep()

    i = i + 1
    if i == 4 then
        break
    end
end

finish()

编辑:增加了赏金,希望能得到一些如何实现这一目标的帮助。

2 个答案:

答案 0 :(得分:2)

lua_resume的返回代码2是LUA_ERRRUN。检查Lua堆栈顶部的字符串以查找错误消息。

类似的模式对我有用,虽然我确实使用coroutine.yield而不是lua_yield而我使用的是C而不是C ++。我不明白为什么你的解决方案不起作用

在你的简历电话中,目前尚不清楚你是否过度简化了一个例子,但我会在你的while循环中进行以下更改:

int status;
status=lua_resume(cL,0);
if (status == LUA_YIELD) {
  printf("loop yielding\n");
}
else {
  running=false; // you can't try to resume if it didn't yield
  // catch any errors below
  if (status == LUA_ERRRUN && lua_isstring(cL, -1)) {
    printf("isstring: %s\n", lua_tostring(cL, -1));
    lua_pop(cL, -1);
  }
}

编辑2:

要进行调试,请在运行简历之前添加以下内容。你有一个字符串被推到堆栈的某个地方:

int status;
// add this debugging code
if (lua_isstring(cL, -1)) {
  printf("string on stack: %s\n", lua_tostring(cL, -1));
  exit(1);
}
status = lua_resume(cL,0);

编辑3:

哦,好悲伤,我不敢相信我已经没有看到这一点,你不想在你要收益时运行luaL_dofile,因为你不能直接屈服于pcall我知道,这是dofile中发生的事情(5.2会传递它,但我认为你仍然需要lua_resume)。切换到:

luaL_loadfile(cL, "scripts/loop.lua");

答案 1 :(得分:0)

上次我搞乱Lua协同程序时,我结束了这样的代码

const char *program =
"function hello()\n"
"  io.write(\"Hello world 1!\")\n"
"  io.write(\"Hello world 2!\")\n"
"  io.write(\"Hello world 3!\")\n"
"end\n"
"function hate()\n"
"  io.write(\"Hate world 1!\")\n"
"  io.write(\"Hate world 2!\")\n"
"  io.write(\"Hate world 3!\")\n"
"end\n";

const char raw_program[] = 
"function hello()\n"
"  io.write(\"Hello World!\")\n"
"end\n"
"\n"
"cos = {}\n"
"\n"
"for i = 0, 1000, 1 do\n"
"  cos[i] = coroutine.create(hello)\n"
"end\n"
"\n"
"for i = 0, 1000, 1 do\n"
"  coroutine.resume(cos[i])\n"
"end";

int _tmain(int argc, _TCHAR* argv[])
{
    lua_State *L = lua_open();
    lua_State *Lt[1000];
    global_State *g = G(L);

    printf("Lua memory usage after open: %d\n", g->totalbytes);

    luaL_openlibs(L);

    printf("Lua memory usage after openlibs: %d\n", g->totalbytes);

    lua_checkstack(L, 2048);

    printf("Lua memory usage after checkstack: %d\n", g->totalbytes);

    //lua_load(L, my_lua_Reader, (void *)program, "code");
    luaL_loadbuffer(L, program, strlen(program), "line");

    printf("Lua memory usage after loadbuffer: %d\n", g->totalbytes);

    int error = lua_pcall(L, 0, 0, 0);
    if (error) {
        fprintf(stderr, "%s", lua_tostring(L, -1));
        lua_pop(L, 1);
    }

    printf("Lua memory usage after pcall: %d\n", g->totalbytes);

    for (int i = 0; i < 1000; i++) {
        Lt[i] = lua_newthread(L);
        lua_getglobal(Lt[i], i % 2 ? "hello" : "hate");
    }

    printf("Lua memory usage after creating 1000 threads: %d\n", g->totalbytes);

    for (int i = 0; i < 1000; i++) {
        lua_resume(Lt[i], 0);
    }

    printf("Lua memory usage after running 1000 threads: %d\n", g->totalbytes);

    lua_close(L);

    return 0;
}

好像你无法加载文件作为协同程序?但是使用函数而应该选择它到堆栈的顶部。

lua_getglobal(Lt[i], i % 2 ? "hello" : "hate");