1

While trying to call a C function from Lua module, using Lua-lanes, the control doesn't transfer to the 'C' function. Is there any problem with which Lua-lanes won't work in a threaded way with an external C dll?

Below is the code snippet

Lua Snippet:

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

function func()
    foo() -- expected to print "Hello world", by 
          -- calling below C function,but not happening
end

C snippet compiled to a dll with VS-2012:

static int foo(lua_state *L)
{
   printf("Hello world\n")
}
4

2 回答 2

1

You are using lanes wrong. This is what you need to do:

function func()
    foo() -- expected to print "Hello world", by 
          -- calling below C function,but not happening
end

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

That should work fine.

于 2013-09-28T15:51:25.510 回答
1

If you want that C function accessible in the new thread then you have to somehow transfer that from the main lua thread over to the new thread when you create the lane. You can do this by using .required from the lua-lane docs.

For example, say you have this simple foomodule:

// foomodule.c
// compiles to 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;
}

And from your lua script:

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

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

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

One possible output:

  calling foo     function: 0x003dff98
  Hello world
于 2013-09-30T20:47:03.430 回答