Everywhere I looked, I saw that the best practice for running lua scripts from many C threads is to have a different lua_State for each thread. Going from this point, be the following script:
local var1
local var2
-- etc.
function onClick(x, y)
-- Process mouse click
end
function onKey(k)
-- Process key stroke
end
-- Do some stuff
while(true) do
-- Do some stuff
end
where onClick and onKey are callback functions called by a Win32 application. These functions are called each one by one C thread. Each one of these have it's own lua_State. Therefore, there is a total of 3 C threads (and 3 lua_States): one for each of the above functions, and the last one for running everything that is not a function (while loop and stuff). So, since Lua can only sees what's in script after lua_dofile and lua_dofile executes everything there is, how can those functions be called without the whole script being executed? I just want one thread to be stuck in the infinite loop; the other ones must wait from the callback functions.
whileloop into a function, and have your C code call it.lua_states are completely separate from each other. You'll need a C API to do sharing, message passing, or whatever you need.lua_newthread. It's supposed to do that exactly what I need: share the same global variables of the mainlua_statewith the child states. Problem is its behavior is crazy.lua_newthreadcreates new coroutines, not OS threads.