lua-users home
lua-l archive

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


Hi all,

Is there any reference on how to use Lua in a heavily-threaded application, with some usage of shared state? I'd expect the game folks do this all the time, but I couldn't find any good examples.
The best I've been able to come up with is the following:  Keep the 
main lua_State object as a singleton, guarded by a mutex.  When the 
mutex is acquired, call lua_newthread to get a new co-routine-like 
thread, store the new thread in a global table so it doesn't get GC'd, 
release the mutex on the global thread, then run some code on the new 
thread.
When the new thread is done, re-acquire the global mutex and remove it 
from the don't-GC-me table.
Would this work?  This doesn't say anything about ITC, but I can worry 
about that later.
I'm a little worried that the GC would interfere with things, if the 
"run a thread" code were called while the main thread were GCing.
Any hints or pointers?

Thanks,

Johann