#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
typedef int bool;
#define true 1
#define false 1
bool running = true;
int lua_finish(lua_State *L) {
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);
lua_State* cL = lua_newthread(L);
luaL_dofile(cL, "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
if (status == LUA_ERRRUN && lua_isstring(cL, -1)) {
printf("isstring: %s\n", lua_tostring(cL, -1));
lua_pop(cL, -1);
break;
}
}
}
lua_close(L);
return 0;
}
In Lua 5.1.4,when call lua_yield(),it thow "attempt to yield across metamethod/C-call boundary" error and return,so when call lua_resume in C it fails, how to fix it?