[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Thread in library which call Lua function
- From: jseb <gmane2010@...>
- Date: Sat, 16 Apr 2011 16:37:49 +0200
> It could be a threading issue. For a quick test, edit llimits.h and
> comment out the defines for lua_lock() and lua_unlock(). Then provide a
Ok, i did so. the assert in "lua_lock" arise, so it's a threading problem.
So this method is not good:
[lua] require library from lua.
[lua] init library thread from lua.
[dll] get the name of the lua's callback in the registry, store it and
store lua context.
[lua] enter in loop, and go out after n seconds.
[dll] while lua is waiting, the thread launched in dll call lua's
callback when needed.
The problem is, Lua is not re-entrant (i know.. several people here said
that to me). So Lua is called when it's in his lock state, and boom.
Better way shall be:
[dll] init lua state (luaL_newstate)
[dll] launch thread
[dll] when needed, call lua function from within the thread
[lua] (i'm called from inside dll)
(all Lua's variables are stored in the lua state of the dll)
I tried this, because i use a method similar to my first attempt with
freeglut.
Freeglut is initialised, and define its redraw callback like a calling
to Lua. So the Freeglut loop is like this:
(sleep n ms) -> call redraw -> call lua function -> rearm freeglut timer
^ |
|_________________________________________________________|
I've got Freeglut sources, and searched for threads inside. No threads,
as far i know. I don't know how Freeglut runs in parallel of my other
code then (as he's sleeping most of time, it should block the other
parts of my program if it wasn't threaded, but it doesn't).
So that's why i can store a Lua's context and its callback redraw
function when using Freeglut. I was wrong trying to mimic this mechanism
with threads.
Thanks to all who answered me, i've learned new things :)