[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Running multiple threads in a lua state
- From: Javier Guerra Giraldez <javier@...>
- Date: Fri, 4 Apr 2014 09:59:18 -0500
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