Re: How to create another lua_State in pthread?

  • From: Hiroaki Nakamura <hnakamur@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Fri, 25 Jan 2013 22:06:50 +0900

Hi, Mike.

Thank you again for your help.

Now I am trying to get the callback function defined in another Lua state
and call pthread_create with the callback address.

https://github.com/hnakamur/luajit-samples/blob/master/pthread/thread2.lua

But it fails with a segmentation fault:

$ export LANG=C
$ luajit thread2.lua
function: 0x40f3bba8
func_ptr        cdata<const void *>: 0x40f3bba8
thread  cdata<uint64_t [1]>: 0x415472e0
thread[0]       0ULL
A res   0
Segmentation fault

Could you tell me how to fix it?
Thanks.

2013/1/25 Mike Pall <mike-1301@xxxxxxxxxx>:
> Hiroaki Nakamura wrote:
>> I tried to typedef lua_State at
>
> Under no circumstances should you need to access the internal
> fields of a state.
>
> Use an opaque struct, just like in lua.h. Here's a simple example
> on how to create another state with the FFI:
>
> local ffi = require("ffi")
>
> ffi.cdef[[
> typedef struct lua_State lua_State;
> lua_State *luaL_newstate(void);
> void luaL_openlibs(lua_State *L);
> void lua_close(lua_State *L);
> int luaL_loadstring(lua_State *L, const char *s);
> int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc);
> ]]
>
> local C = ffi.C
>
> local L = C.luaL_newstate()
> assert(L ~= nil)
> C.luaL_openlibs(L)
> assert(C.luaL_loadstring(L, 'print("Hello from another Lua state!")') == 0)
> assert(C.lua_pcall(L, 0, 1, 0) == 0)
> C.lua_close(L)
>
> --Mike
>



-- 
中村 弘輝 )Hiroaki Nakamura) hnakamur@xxxxxxxxx

Other related posts: