From: John L. <jla...@gm...> - 2006-08-09 22:15:41
|
On 8/9/06, he...@gm... <he...@gm...> wrote: > Hello, > I'd like to use wxLua in a multi-threading application, several threads should host their own Lua instance. > The Lua instances should run independent from the others, not sharing anything. > So I create a wxLuaState object for every thread and use RunFile to execute the script. No problems, except that any GUI functions MUST be made from the main thread. This is a limitation of wxWidgets due to the underlying gui system which is true for pure C++ code as well.. > But: I also want to use own functions (published through RegisterFunction) and I can't manage to get them independent. > I would have to declare them in a global scope (or as static member functions), both would lead to a common function > implementation which is called from all threads. But I want to have them separate, to avoid any interference between the Lua threads. > > Can anybody give me a hint how to do this? I think you're wondering how to have the underlying C function independent for each thread since each wxLuaState::RegisterFunction(...) call for a given lua state pushes the function independently into the lua_State already. Simple case: Just have multiple C functions if you'll only have a small known number of them. More complicated: I am not very well versed in C threading, but I believe that you will have to have mutexes (and/or conditions?) for each wxLuaState you create (maybe a wxList mapping between the lua_State of the wxLuaState and it's mutexes, keyed on the memory location of the lua_State?), then look up from within the C function which lua_State is "active" and do the mutex thing in there. In other words lua doesn't know about the threading and the C functions handle it all internally. I hope this makes some sort of sense, John Labenski |