无法在Lua-lanes中调用C函数。

在尝试使用Lua-lanes从Lua模块调用C函数时,控制不会转移到C函数。是否存在了Lua-lanes无法与外部C dll一起以线程方式工作的问题?

下面是代码片段

Lua代码:

lanes.gen("*",func)
thread = func()
thread:join()

function func()
    foo() -- 预计打印“Hello world” ,通过
          -- 调用下面的C函数,但没有实现
end

C代码编译为VS-2012中的dll:

static int foo(lua_state *L)
{
   printf("Hello world\n")
}
点赞
用户2675813
用户2675813

你在错误地使用 lanes 。你需要这样做:

function func()
    foo() -- 预期可以通过调用以下的C函数打印“Hello world”,但实际上并没有发生
end

local launcher = lanes.gen("*", func)
thread = launcher()
thread:join()

这样应该可以正常工作。

2013-09-28 15:51:25
用户234175
用户234175

如果你想在新线程中访问 C 函数,那么你就需要在创建 Lane 时将其从主线程传递到新线程中。你可以使用 lua-lane 文档 中的 .required 来实现此功能。

例如,假设你有一个简单的 foomodule:

// foomodule.c
// 编译为 foomodule.dll
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"

static int foo(lua_State *L)
{
  printf("Hello world\n");
  return 0;
}

int luaopen_foomodule(lua_State *L)
{
  lua_pushcfunction(L, foo);
  lua_pushvalue(L, -1);
  lua_setglobal(L, "foo");
  return 1;
}

然后在你的 Lua 脚本中:

-- footest.lua
lanes = require 'lanes'.configure()

function func()
  print("calling foo", foo)
  return foo()
end

thr = lanes.gen("*", {required = {'foomodule', }}, func)
thr():join()

可能的输出如下:

calling foo     function: 0x003dff98
Hello world
2013-09-30 20:47:03