直接上代码
cb.c
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" static int lua_watch = LUA_REFNIL; static lua_State *ud = NULL; void doit() { int time=1; for (int i=0;i<2;i++) { printf(">>> doit sleep %d s start \n",time); sleep(time); printf(">>> doit sleep over \n"); lua_State *L = ud; lua_rawgeti(L, LUA_REGISTRYINDEX, lua_watch); int a=1; int b=2; int c=3; lua_pushinteger(L, a); lua_pushinteger(L, b); lua_pushinteger(L, c); lua_call(L, 3, 0); } } static int lsetwatch(lua_State *L){ printf(">>> setwatch \n"); lua_watch = luaL_ref(L, LUA_REGISTRYINDEX); printf(">>> setwatch lua_watch=%d \n",lua_watch); ud = L; pthread_t tid; pthread_create(&tid, NULL, &doit, NULL); return 0;