2013-09-27 35 views
1

嘗試使用Lua通道從Lua模塊調用C函數時,控件不會傳輸到「C」函數。有沒有任何問題,Lua通道不能以外部C dll的線程方式工作?無法從Lua通道調用C函數

下面是代碼片段

的Lua段:

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

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

Ç代碼段編譯爲一個dll與VS-2012:

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

回答

1

如果您希望在新線程中可以訪問C函數,那麼當您創建通道時,必須以某種方式將主函數從主線程轉移到新線程。您可以使用lua-lane docs中的.required來完成此操作。

例如,假設你有這樣的簡單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; 
} 

從你的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 
+1

感謝您的輸入,通過從新通道加載foomodule我可以調用「C」功能 – Anoop

1

您正在使用的車道錯了。這是你需要做的:

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() 

這應該工作正常。