lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Monday 13, Digital wrote:
> How can I pass a lua state in a struct to a function or thread in c/c++?
> 
> I tried:
> 
> //----------------------
> lua_State *L;
> 
> struct sargs {
>   lua_State *L;
>   char *arg2;
> };
> 
> void *runlua(void *arg)
> {
> char *dat;
> struct sargs *args = (struct sargs*)arg;
> lua_State *L = (lua_State *) args->L;
> dat = args->arg2;
> return NULL;
> }
> 
> int main()
> {
> // Init the regular lua stuff:
> 
> struct sargs pargs;
> pargs.L = L;
> pargs.arg2 = "pass data";
> 
> return 0;
> }
> 
> But with that I am unable to retrieve the lua state. Or through a
> created thread passing the struct as argument.


This seems more like a C question then a Lua question.  But you can see an 
example here [1] of passing a Lua state to a thread.

If you just want to use threads from Lua try lua-llthreads [2].  See the 
example [3] that uses llthreads.

1. https://github.com/Neopallium/lua-
llthreads/blob/master/src/thread.nobj.lua#L180
2. https://github.com/Neopallium/lua-llthreads
3. https://github.com/Neopallium/lua-
llthreads/blob/master/examples/sub_threads.lua

-- 
Robert G. Jakabosky