c回调lua测试

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码

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;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值