lua-users home
lua-l archive

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


On 3/9/2012 5:28 PM, Michal Kottman wrote:
> Hello list,
>
> I'd like to know your opinion and experience with handling
> multithreading in Lua. My main question is - how to handle
> multithreading that is forced onto me by the library I am binding?

The situation in lgi (Lua gobject binding) is very similar, I have to assume that any kind of gobject->Lua callback can come in any thread. To solve this, I introduced per-lua_State lock (yes, a GIL). Before calling gobject method from Lua, the lock is unlocked and locked back when the gobject (C) call returns and before unmarshalling of output values begin. Similarly, when a callback (i.e. gobject->Lua call) is received, the lock is locked, and before returning back to gobject (C) it is unlocked back.

So far I haven't encountered any kind of problem with this scheme, Lua itself seems to be happy to run in 'switching threads'.

Pavel