直接上代码
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;

本文展示了一个C语言实现回调到Lua的测试案例,通过代码示例解释了如何在C中调用Lua脚本并执行回调操作。主要内容包括cb.c文件、test.lua脚本以及对应的makefile编译规则。
最低0.47元/天 解锁文章
4280

被折叠的 条评论
为什么被折叠?



