lua-users home
lua-l archive

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


Thank you for your response, it is very helpful. Do you think that having many lua states has memory overhead that may be optimized in some way?

Thanks,
Milind




On Fri, Apr 4, 2014 at 7:59 AM, Javier Guerra Giraldez <javier@guerrag.com> wrote:
On Fri, Apr 4, 2014 at 9:49 AM, Milind Gupta <milind.gupta@gmail.com> wrote:
> I think that it would be more memory efficient if I create just 1 lua state
> and run all the threads in that, each with its own environment. Is that
> true?


no

the issue is not about the environment, but about sharing the whole
state.  it's a complex memory data structure fairly optimized for
single-threaded access.  it's definitely _not_ threadsafe as is.

it's not too hard to wrap every access to the state with mutex locks,
but then i amount to a GIL (global interpreter lock), which makes all
Lua code serialized and with a heavy lock/unlock overhead.

long time ago, I dabbled on lock-free datastructures and managed to
create a few that could be used from Lua, but they're much harder to
get right than 'traditional' lock-based threadsafe programming.  In
the end, it's better to have several Lua states that communicate by
message passing.

just my 2¢

--
Javier